12
$\begingroup$

How do I define the syntax coloring of local variable of function myfunc so that it only colors the variable that is not in a List?

For example,

SyntaxInformation[myfunc] = {"LocalVariables" -> {"Integrate", {2,2}}}

leads to the following syntax coloring:

enter image description here

I only want coloring like in the first line. I don't want the coloring appearing in the second line. In the second case, it should not be colored, and should just look like:

enter image description here

$\endgroup$
  • $\begingroup$ This is probably not what you are looking for, but you might find it interesting How to change the font color of a variable in whole input cell $\endgroup$ – Sumit May 28 '16 at 12:32
  • $\begingroup$ @Sumit You're right; I'm not interested in that solution. I need something that works for a package function. $\endgroup$ – QuantumDot May 28 '16 at 12:36
  • $\begingroup$ Could you explain further what is the goal? From your description I though that z should be colored, but it isn't. $\endgroup$ – Kuba Jun 6 '16 at 8:35
  • $\begingroup$ @Kuba In the function myfunc, only the second argument is a local variable. myfunc only takes a symbol in the second argument. It does not take a List in the second argument. Therefore, the second argument should be colored as a local variable only if it is a symbol, and not if it is a list. In the example above, the local variable is x and should be colored if it appears in the second argument. But if a List appears in the second argument, the x inside the List should not be colored. How do I prevent x from being colored if it is in a list? $\endgroup$ – QuantumDot Jun 6 '16 at 10:58
  • 2
    $\begingroup$ Would SyntaxInformation[myfunc] = {"ArgumentsPattern" -> {_, {}, ___} help for your purpose? $\endgroup$ – mmeent Jan 9 '18 at 8:57
8
+100
$\begingroup$

Here is my argument for why I think this is not possible:

First, we'll gather all the SyntaxInformation out there and GroupBy key:

sinfoAll =
  Association@
   ToExpression[
    Join[
     Names["*`*"],
     Names["*`*`*"]
     ],
    StandardForm,
    Function[Null,
     If[Length@OwnValues[#] > 0 || System`Private`HasOwnCodeQ[#],
      Nothing,
      # ->
       SyntaxInformation[#]
      ],
     HoldAllComplete
     ]
    ];

sinfoGood =
  DeleteCases[sinfoAll, {} | $Failed];

sinfoVals =
  GroupBy[DeleteDuplicates@Flatten@Values@sinfoGood, First -> Last];

Then looking at the "LocalVariables":

sinfoVals["LocalVariables"]

{None, {"Manipulate", {2, \[Infinity]}, 
  "Lexical"}, {"Solve", {2}}, {"Plot", {2, 3}}, {"Plot", {2, 
   4}}, {"D", {2, \[Infinity]}}, {"Table", {2, \[Infinity]}}, \
{"Solve", {2, \[Infinity]}}, {"Integrate", {2, \[Infinity]}}, \
{"Limit", {2}}, {"Plot", {3, \[Infinity]}}, {"Plot", {2}}, \
{"Manipulate", {2}}, {"Plot", {2, \[Infinity]}}}

We can try each of these in turn:

Block[{m = 1},
  AssociationMap[
   Function[
    With[{s = ToExpression["f" <> ToString[m++]]},
     SyntaxInformation[s] = {"LocalVariables" -> #};
     {
      Style[s[x*y*z, x, y, z], "Input"],
      Style[s[x*y*z, { x, y, z}], "Input"]
      }
     ]
    ],
   sinfoVals["LocalVariables"]
   ]
  ] // Dataset

asdasd

And we find that the opposite of what you want seems to be possible, but what you want does not.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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