xuhdev
xuhdev

Reputation: 9360

Emacs: is there a debug mode I can check where the value of a variable is changed?

Is there a debug mode I can check where (which line in which file) the value of a variable is changed in Emacs?

Upvotes: 2

Views: 748

Answers (3)

RichieHH
RichieHH

Reputation: 2123

https://www.gnu.org/software/emacs/manual/html_node/elisp/Variable-Debugging.html

Command: debug-on-variable-change variable ¶

This function arranges for the debugger to be called whenever variable is modified.

It is implemented using the watchpoint mechanism, so it inherits the same characteristics and limitations: all aliases of variable will

be watched together, only dynamic variables can be watched, and changes to the objects referenced by variables are not detected. For details, see Running a function when a variable is changed..

Upvotes: 0

rofrol
rofrol

Reputation: 15266

in Emacs 26.1 do M-x debug-watch <variable> RET

https://emacs.stackexchange.com/questions/27962/tracking-down-a-write-to-a-variable

Upvotes: 1

sds
sds

Reputation: 60044

I am pretty sure you are out of luck. However, not all is quite lost.

Global Variable

With Common Lisp you could use define-symbol-macro, but Emacs Lisp does not have it.

You need to eval

(defun my-func-name ()
  my-var-name)

and

(defsetf my-func-name (val)
  (warn "my-var-name=%s" val) ; or `error'
  (setq my-var-name val))

Then you have to search and replace my-var-name with (my-func-name) in the sources, also replacing (setq my-var-name ...) with (setf (my-func-name) ...) and recompile and reload the sources.

Local Variable

Replace let with symbol-macrolet.

Upvotes: 1

Related Questions