Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Mathematica package to track which symbols may reference a given symbol
BeginPackage["ReferenceTracker`"]
TrackReferences;
Begin["`Private`"]
Clear[$globalProperties];
$globalProperties = {
OwnValues,
DownValues,
SubValues,
UpValues,
NValues,
FormatValues,
Options,
DefaultValues,
Attributes,
Messages
};
ClearAll[getDefinitions];
SetAttributes[getDefinitions,HoldAllComplete];
getDefinitions[s_Symbol]:=
Flatten @ Through[
Map[
Function[prop,Function[sym,prop[sym],HoldAll]],
$globalProperties
][Unevaluated[s]]
];
ClearAll[TrackReferences];
SetAttributes[TrackReferences, HoldFirst];
TrackReferences[s_Symbol, context_String]:= TrackReferences[s, {context}];
TrackReferences[s_Symbol, contexts:{___String}]:=
With[{allNames = Join @@ Map[Names[#<>"*"]&, contexts]},
Select[allNames,
Function[symName,
ToExpression[
symName,
StandardForm,
Function[sym,!FreeQ[ getDefinitions[sym], HoldPattern[s]], HoldAllComplete]
]
]
]
];
End[]
EndPackage[]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.