How to get the initial value of a variable in Elisp?

Is there a function in Emacs Lisp to get the initial value of a character initialized by defvar ? Like some-function shown below:

 (defvar var "initial value") (setq var "changed value") (some-function 'var) => "inital value" 
+4
source share
5 answers

Emacs does not remember the initial value. If you rate

 (defvar var "initial value") (setq var "changed value") 

in buffer *scratch* , "initial value" no longer available, full stop.

If defvar was executed in the downloaded file, Emacs remembers where it is loaded from; (symbol-file var 'defvar) returns the file name, and you can get the original expression in which the variable was initialized (but not the original value), assuming the file still exists. This is also available using the Mx find-variable command .

+6
source

This is usually not possible (Emacs does not remember the original value), but there are some exceptions.

defcustom variables

Variables defined using defcustom and changed using the configuration system receive the initial value, the saved value, and the configured, but not yet saved value as properties:

 (defcustom foo 0 "testing") (custom-set-variables '(foo 1)) (setq foo 2) (customize-mark-as-set 'foo) (setq foo 3) (car (get 'foo 'standard-value)) ;; evaluates to 0 (car (get 'foo 'saved-value)) ;; evaluates to 1 (car (get 'foo 'customized-value)) ;; evaluates to 2 foo ;; evaluates to 3 

See the section Defining configuration variables in the elisp manual, in particular the discussion of the documentation for the custom-reevaluate-setting function above.

buffer-local variables

Buffered local variables have a default value (global) and a local buffer value, which may differ from the global value. You can use the default-value function to get the default-value :

 (setq indent-tabs-mode nil) (default-value 'indent-tabs-mode) ;; evaluates to t 

However, you can change the default value, and Emacs will not remember the original default value:

 (set-default 'indent-tabs-mode nil) (default-value 'indent-tabs-mode) ;; evaluates to nil 

For more information, see Introduction to Buffer-Local Variables in the elisp manual.

+10
source

If the variable is set to local buffer values or frame local values , try:

 (default-value 'var) 

Although, if someone used setq-default to change the setq-default value, you will get a new one, not the original, that was configured through defvar . From the doc:

This function returns the default character value. This value is visible in buffers and frames that do not have their own values ​​for this variable. If the character is not a buffer-local character, this is equivalent to the character value (see Accessing Variables ).

+4
source

It seems that @ tao-peng found the answer itself here, but the full story is this:

A character created with defvar usually only has the value it is set to, as well as an optional property list, which usually has the value variable-documentation or possibly risky-local-variable , for example:

 > (symbol-plist 'load-path) (risky-local-variable t variable-documentation -587478) 

On the other hand, a character created with defcustom has a much longer symbol-plist , including the standard-value property, which you can do this:

 > (get 'package-archives 'standard-value) ((quote (("gnu" . "http://elpa.gnu.org/packages/")))) 

And as @ trey-jackson notes, in the case of characters having local or local buffer-local values, you can get the original value with

 > (setq foo "bar") > (make-variable-buffer-local 'foo) > (setq foo "baz") > (default-value 'foo) "bar" > foo "baz" 
+3
source

Our solution:

 ;;;###autoload (defun xorns-get-original-value (symbol) "Return SYMBOL original value or nil if that is void." (if (boundp symbol) (eval (car (get symbol 'standard-value))))) 
0
source

All Articles