Mathematica has a useful feature that for functions and special constructs getting passed local variables (for example Minimize
or Block
), the affected variables are shown in a different colour. Now when writing your own functions of this type (like the NOptimize
function found in this answer), Mathematica of course doesn't know the meaning of the arguments and doesn't change the colours accordingly.
Is there a way to tell Mathematica that an argument is a set of variables to affect other arguments?
To make this post self-contained, here a small (admittedly silly) example:
SetAttributes[SequenceVars,HoldAll]
SequenceVars[vars_List,expr_]:=Block[vars,vars=Range@Length@vars;expr]
(* add syntax highlighting somehow *)
SequenceVars[{a,b,c},foo[a,b,c,d]]
(* here a, b and c, but not d, should appear in a different colour while typing *)