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.

Malabarba
  • 22,878
  • 6
  • 78
  • 163
cass
  • 53
  • 3
  • 2
    Related: http://emacs.stackexchange.com/q/3022/50. (I've edited your title to not be a duplicate of it). – Malabarba Apr 20 '15 at 10:59

3 Answers3

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.

Malabarba
  • 22,878
  • 6
  • 78
  • 163
  • ...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.)

Drew
  • 75,699
  • 9
  • 109
  • 225
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.

abo-abo
  • 13,943
  • 1
  • 29
  • 43