tags:

views:

63

answers:

2

In Emacs Lisp, is there a function to get the initial value of a symbol initilized by defvar? Like the some-function shown below:

(defvar var "initial value")
(setq var "changed value")
(some-function 'var)
=> "inital value"
+2  A: 

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

(default-value 'var)

Though, if someone has used setq-default to change the default, you'll get the new one, and not the original that was set up via defvar. From the documentation:

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

Trey Jackson
`default-value` seems to return a buffer local variable's global value, not the initial value.
Tao Peng
@pt True, I immediately jumped to buffer-locality in my head, I've updated the answer to reflect as much.
Trey Jackson
+3  A: 

Emacs doesn't remember the initial value. If you evaluate

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

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

If the defvar was performed in a loaded file, Emacs remembers where it's loaded from; (symbol-file var 'defvar) returns the file name, and you can get an the original expression that the variable was initialized with (but not the original value), assuming the file is still around. This is also available via the command M-x find-variable.

Gilles
Accepted. Luckly I found the desired variables are defined by `defcustom` instead of `defvar`. `defcustom` stores symbol's standard value in its property list.
Tao Peng