5

Is there a (quick) way of resetting a variable to its default value (as defined in some .el file) after it has been modified? e.g. via commands in the .emacs file, without having to restart emacs.

Example: Set ispell-tex-skip-alists to the value as defined in `ispell.el' after modification.

5

Here you go:

(defun endless/reset-var-at-point ()
  "Revaluate the definition of variable at point."
  (interactive)
  (save-window-excursion
    (save-excursion
      (let ((buf (current-buffer)))
        (find-variable-at-point)
        (when (looking-at-p "(def\\(var\\|custom\\|const\\) ")
          (eval-defun nil))
        (unless (equal buf (current-buffer))
          (bury-buffer (current-buffer)))))))

As others have stated, the initial value of a defvar is not stored anywhere. However, the location of its definition does get stored by Emacs, so you can visit it and reevaluate it.

Note that this will only work if the location of the variable is known, and if it is defined in a conventional way (not as part of a wierd macro or something).
It will also leave a buffer hanging around.

So it is very much not designed for used in lisp code. For interactive use, it should do just fine.

  • ...in which I learn that C-M-x (eval-defun) in a variable (or face) definition actually resets its value (unlike other forms of re-evaluation, which do not). – phils Apr 20 '15 at 22:55
4

No. Non-custom variables need not even have a default value.

Except if you mean a global value as opposed to a buffer-local value. In the case of a variable that has buffer-local values, yes, you can use function default-value to obtain its default value, and you can then use setq to set any buffer-local value to that default value. (You can also use set-default-value to change the default value.)

1

It's possible if the variable was declared as a defcustom:

(eval (car (get 'ediff-split-window-function 'standard-value)))
;; => split-window-vertically

But ispell-tex-skip-alists was declared as defvar, so this isn't possible.

  • 1
    Technically, you can visit the variable definition and reevaluate it entirely with elisp code. – Malabarba Apr 20 '15 at 10:55
  • True, but there's no introspection-style option. – abo-abo Apr 20 '15 at 11:03
  • The question explicitly asks about a non-custom variable. – Drew Apr 20 '15 at 15:03
  • 2
    The question was edited two hours after I provided an answer. – abo-abo Apr 20 '15 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.