It is sometimes useful to take some action when a variable changes its value. The variable watchpoint facility provides the means to do so. Some possible uses for this feature include keeping display in sync with variable settings, and invoking the debugger to track down unexpected changes to variables (see Variable Debugging).
The following functions may be used to manipulate and query the watch functions for a variable.
function add-variable-watcher symbol watch-function
This function arranges for
watch-function to be called whenever
symbol is modified. Modifications through aliases (see Variable Aliases) will have the same effect.
watch-function will be called, just before changing the value of
symbol, with 4 arguments:
symbol is the variable being changed.
newval is the value it will be changed to. (The old value is available to
watch-function as the value of
symbol, since it was not yet changed to
operation is a symbol representing the kind of change, one of:
where is a buffer if the buffer-local value of the variable is being changed,
function remove-variable-watcher symbol watch-function
This function removes
symbol’s list of watchers.
function get-variable-watchers symbol
This function returns the list of
symbol’s active watcher functions.
There are a couple of ways in which a variable could be modified (or at least appear to be modified) without triggering a watchpoint.
Since watchpoints are attached to symbols, modification to the objects contained within variables (e.g., by a list modification function see Modifying Lists) is not caught by this mechanism.
Additionally, C code can modify the value of variables directly, bypassing the watchpoint mechanism.
A minor limitation of this feature, again because it targets symbols, is that only variables of dynamic scope may be watched. This poses little difficulty, since modifications to lexical variables can be discovered easily by inspecting the code within the scope of the variable (unlike dynamic variables, which can be modified by any code at all, see Variable Scoping).