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$ 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$
3
  • 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$ Commented Nov 3, 2015 at 12:41
  • $\begingroup$ this behavior is different in 14.1.0, Options[g] = {foo -> 1}; SyntaxInformation[g] = {"ArgumentsPattern" -> {OptionsPattern[]}}; now highlights options other than foo without using "OptionNames" $\endgroup$ Commented Sep 19, 2024 at 0:11

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.