TL;DR; with a minimal example
Don't know how to make coloring in e.g. Plot
work (Module
and friends work ok).
Don't know how to make coloring of System`
and other contexts different.
Otherwise:
With[{autoSO = Options[$FrontEnd, AutoStyleOptions]},
DynamicModule[{},
ExpressionCell[
Defer@Module[{n = 1},
Plot[Sin[x/n], {x, 0, 1}, PlotLabel -> "Label"]], "Notebook",
"Input"]
,
Initialization :> (
CurrentValue[$FrontEndSession, ShowSyntaxStyles] = True;
SetOptions[$FrontEndSession, autoSO];
CurrentValue[$FrontEndSession, {AutoStyleOptions,
"UndefinedSymbolStyle"}] = {FontColor -> Black};
)
]
]
You can put in in NotebookDynamicExpression
, Initialization
, or in a Button
to trigger it only when needed.
Details
(*notebook we will deploy as cdf*)
Checkbox[Dynamic@CurrentValue[$FrontEndSession, ShowSyntaxStyles]]
With[{autoSO = Options[$FrontEnd, AutoStyleOptions]},
Button["Get MMA's colors", SetOptions[$FrontEndSession, autoSO]]
]
Button["Fix System` colors",
CurrentValue[$FrontEndSession, {AutoStyleOptions,
"UndefinedSymbolStyle"}] = {FontColor -> Black}
]
GlobalVariable = "String"
Plot[Sin[x], {x, 0, 1}, PlotLabel -> "Label"]
I though that we can't do much because after copying Default and Core stylesheets to appropriate Player's directories nothing changed.
But there are couple of steps which will lead us to something useful.
ShowSyntaxStyles
options is special because its value for a $FrontEnd*
has higher precedence than Stylesheet's or Notebook's. So we can set it manually for a $FrontEnd*
then.
Better! Not perfect though, everything is blue and has default styles one may have tweaked in Mathematica.
We can inherit custom styles (like Strings' here) by injecting MMA $FrontEnd
settings and applying them in a CDF, e.g.:
With[{autoSO = Options[$FrontEnd, AutoStyleOptions]},
Button["Get MMA's colors", SetOptions[$FrontEndSession, autoSO]]
]
after clicking:
Better, but still the problem with blue symbols remain
I thought it can be fixed with
AutoStyleOptions -> "SymbolContextStyles" -> {
"System`" -> {}, "Global`" -> {}, Automatic -> {}
}
which we can use to e.g.
Button["Fix System` colors",
CurrentValue[$FrontEndSession, {AutoStyleOptions,
"SymbolContextStyles", Automatic}] = {FontColor -> Black}
]
but it is not the case, $FrontEnd
in the Player doesn't know that Plot
is a System`'s
symbol. Even if we use:
FrontEndExecute @ FrontEnd`Private`GetUpdatedSymbolContexts[];
Update/refresh notebook variable color on clear
So at the end I think it is ok to change color of undefined symbols to black, the lesser evil:
CurrentValue[$FrontEndSession, {AutoStyleOptions,
"UndefinedSymbolStyle"}] = {FontColor -> Black}