Tao Peng
Tao Peng

Reputation: 2718

How can I get a variable's initial value in Elisp?

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"

Upvotes: 6

Views: 5119

Answers (5)

Medardo Rodriguez
Medardo Rodriguez

Reputation: 19

Our solution:

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

Upvotes: 1

Richard Hansen
Richard Hansen

Reputation: 54153

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

defcustom variables

Variables defined with defcustom and modified with the customization system get the original value, saved value, and customized-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 Defining Customization Variables section in the elisp manual, specifically the discussion above the documentation for the custom-reevaluate-setting function.

buffer-local variables

Buffer-local variables have a default (global) value and a buffer-local value that may differ from the global value. You can use the default-value function to obtain the default value:

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

It is possible to change the default value, however, and Emacs won't remember the original default value:

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

See the Introduction to Buffer-Local Variables section in the elisp manual for more details.

Upvotes: 16

aculich
aculich

Reputation: 14855

It looks like @tao-peng found the answer himself here, but the full story is this:

A symbol created with defvar typically only has the value it is set to, as well as optionally a property list which at most usually has variable-documentation or maybe risky-local-variable set, e.g.:

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

On the other hand, a symbol created with defcustom has a much longer symbol-plist including a standard-value property that you can get like this:

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

And as @trey-jackson notes, in the case of symbols that have buffer-local or frame-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"

Upvotes: 3

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.

Upvotes: 6

Trey Jackson
Trey Jackson

Reputation: 74430

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).

Upvotes: 5

Related Questions