26

Is there any way to have the same syntax coloring with SyntaxInformation that Block has?

SyntaxInformation[func] = {"ArgumentsPattern" -> {{__}, _},
                        "LocalVariables" -> {"Solve", {1, Infinity}}};
func[vars : {__}, body_] := {};

After evaluating the above definition, the following two lines have different coloring:

func[{parameter, variable, constant = 0}, parameter + variable + constant]

Block[{parameter, variable, constant = 0}, parameter + variable + constant]

Mathematica graphics

As you can notice, any local variable with an initial value is not highlighted as local. I use the default syntax coloring, no changes were applied. SyntaxInformation accepts the following values: {"Table", "Solve", "Integrate", "Limit", "Plot", "Manipulate"} none of which produces the same behaviour as Block does. Perhaps someone knows about an undocumented option value?

István Zachar
  • 47,032
  • 20
  • 143
  • 291
  • 2
    +1. I was wondering about the same thing, for my LetL. Did not find the answer yet. – Leonid Shifrin Dec 12 '13 at 14:58
  • 1
    Maybe have a look at GetFEKernelInit.tr, which has a section called Syntax coloring and other syntax information. It's not very detail, but might provide some clues. – Silvia Dec 12 '13 at 19:23
  • @Silvia Thanks for the pointer. Unfortunately, the syntax-coloring code in th .tr file refers to another textresource file (FunctionInformation2.m) containing syntax and usage information for almost all symbols, but not for Block, Module, With or DynamicModule. All their "LocalVariables" information is missing. The only new type for "LocalVariables" that I could found is "D" which might be identical to "Integrate". – István Zachar Dec 12 '13 at 21:45
  • @IstvánZachar In FunctionInformation.m there is a line {"Module", {{__}, _}}, but nothing more. Guess we'll have to wait for someone from WRI to answer this puzzle.. (Strange I didn't get the comment notice. Sorry for reply late.) – Silvia Dec 14 '13 at 02:09
  • 2
  • Why the OP's question claims: "As you can notice, any local variable with an initial value is not highlighted as local."? If we evaluate func[...constant=0...], the value of constant is going to be defined as 0 in the notebook, so it is not local and should not be colored as local. – Stitch Nov 29 '16 at 01:10
  • @Stitch So why does Block color constant as a local variable then? Semantically, constant is not a local variable of course, but syntactically it is. I omitted the definition of func for sake of simplicity, but it is meant to be a scoping function which holds and localizes variables listed in its first argument. Even if you implement it as Block, syntax coloring won't be right. – István Zachar Nov 29 '16 at 09:46
  • Just for curiousity: how do you implement the same underlying behaviour in func as Block has. (So that func won't assign the provided value to the symbol constant, leaving it undefined after usage.) EDIT: With an attribute like HoldFirst? – Gyebro Nov 30 '16 at 15:35
  • 1
    @Gyebro Yes, you have to use HoldAll or similar. See for example Leonid's LetL here which is a scoping construct similar to With. – István Zachar Dec 01 '16 at 15:48
  • 1
    FYI, I've asked WRI Support about that but no answer so far. – Kuba Dec 12 '16 at 12:36
  • I've tried scraping the "SystemFiles" directory for any symbol containing Syntax in their name, but got nothing that gives new SyntaxInformation forms. I'll try expanding the scrape and including strings that have "Syntax" too. – b3m2a1 Dec 15 '16 at 11:30

1 Answers1

12

Unfortunately, the current answer is "you can't". These are hard coded into the FE. Module/With, in particular, are not treated as local variables since they are really lexically scoped. If you look carefully, you'll see they are a slightly different color, and if you look in the syntax coloring prefs you'll see they have a dedicated section.

One of our own iternal wishlist items is to unify the implementation of these different color schemes as well as allowing new rules to be written in Wolfram Language so users can define they own local variable rules. As with everything, this is just one of many competing features. If this sounds like something you would want, I suggest you contact support with your request. The more evidence we have that users are interested in this, the more likely it is we will spend the resources needed to make it happen.

Itai Seggev
  • 14,113
  • 60
  • 84