1

I'm trying to find a way to run a function whenever a specific variable is accessed or read in Emacs Lisp. I want to know which functions are using this variable, but I don't know them beforehand.

I'm aware of the add-variable-watcher function, which can be used to monitor when a variable is modified. However, this doesn't seem to cover the case when the variable is simply accessed or read without being modified.

Is there a way to run a "get" function or a hook whenever a variable is read in Emacs Lisp, without knowing which functions are using the variable?

4
  • You might want to take a look at the C function eval_sub. Commented Dec 20, 2023 at 11:24
  • emacs.stackexchange.com/tags/elisp/info Commented Dec 20, 2023 at 17:03
  • If the variable is one you created (e.g. for your own package or other code), consider replacing its occurrences with a nullary function call that just returns the variable's value. ;-) Commented Dec 20, 2023 at 17:06
  • M-x rgrep is your friend -- just search the code if you want to know what code is using some symbol. (It's possible that something will generate the name in a less-obvious way, but for the most part names are not obfuscated and we can leverage basic tooling for things like this). Commented Dec 20, 2023 at 21:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.