14
$\begingroup$

Suppose I have a function like

Options[f] = {foo -> 1};
f[opt : OptionsPattern[{f,g}]] := h@g[Sequence @@ FilterRules[{opt}, Options[g]]]

I purposefully do not want to append Options[g] to Options[f] because I want the defaults to be inherited from g instead of setting them separately for f.

In this situation, is there a way to use SyntaxInformation with option highlighting for f? It seems a naïve attempt results in red colouring for anything that is not in Options[f] (as the documentation states). But I wanted to ask anyway, just in case I missed something and there's a way to get this working after all.

Example:

Options[g] = {bar -> 2}; (* in practice g could be a builtin *)
SyntaxInformation[f] = {"ArgumentsPattern" -> {OptionsPattern[{f, g}]}};

Mathematica graphics

So it appears that arguments of OptionsPattern are not supported within SyntaxInformation (they are ignored).

$\endgroup$
2
  • $\begingroup$ Or perhaps people consider it bad practice to want to inherit the default option values from g? If so, why? $\endgroup$
    – Szabolcs
    Commented Oct 20, 2015 at 8:40
  • 1
    $\begingroup$ I use a different approach to inheriting default options here. I think this is not directly useful to you, but it may still interest you and your feedback would be appreciated. $\endgroup$ Commented Oct 20, 2015 at 11:17

1 Answer 1

14
$\begingroup$

SyntaxInformation accepts undocumented "OptionNames" property. Using it we can explicitly define list of option names that will not be colored red.

ClearAll[f]
Options[f] = {foo -> 1};
SyntaxInformation[f] = {
    "ArgumentsPattern" -> {OptionsPattern[]},
    "OptionNames" -> {"bar"}
};

f with bar

With simple helper function extracting relevant option names:

optionNames = ToString /@ Apply[Join, Options /@ {##}][[All, 1]] &;

ClearAll[f, g]
Options[f] = {foo -> 1};
Options[g] = {bar -> 2};
SyntaxInformation[f] = {
   "ArgumentsPattern" -> {OptionsPattern[]}, 
   "OptionNames" -> optionNames[f, g]
};

f with foo and bar

Tested in Mathematica versions: 8.0, 9.0, 10.0, 10.1, 10.2 and 10.3.

$\endgroup$
2
  • 1
    $\begingroup$ +1, great find! Did you find this by doing PrintDefinitions@System`Utilities`GetSystemSyntaxInformation? $\endgroup$ Commented Nov 3, 2015 at 12:19
  • $\begingroup$ @JacobAkkerboom No, I was looking at SyntaxInformation@StyleBox and there it was. I never seen System`Utilities`GetSystemSyntaxInformation, thanks for showing it. $\endgroup$
    – jkuczm
    Commented Nov 3, 2015 at 12:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.