Skip to content

Instantly share code, notes, and snippets.

@lshifr
Created April 29, 2016 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lshifr/bad6c47bc9ab3fbe4fb2bcfe0fbca6a3 to your computer and use it in GitHub Desktop.
Save lshifr/bad6c47bc9ab3fbe4fb2bcfe0fbca6a3 to your computer and use it in GitHub Desktop.
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