How does AutoStyleWords work?

The title says it all. I assume it provides some way of automatically applying a certain style, or a list of options, to certain words written, just like other options of the sort like AutoItalicWords. But I haven't been able to make it work...

Thanks!

-
I tried to change that setting and it crashed Mathematica. :-O –  Mr.Wizard Mar 6 '12 at 9:48

A very similar question came up internally at WRI, so I have a nearly ready-made answer. In that case, the fellow wanted to highlight certain loop constructs, like Do and For in his code automatically. Here's how I responded.

In a fresh notebook, Format->Edit Stylesheet..., then paste and interpret the cells below at the bottom of the stylesheet. And voila, you'll got purple Dos and Fors in the notebook the stylesheet modified.

{
Cell[StyleData["Input"],
AutoStyleWords->{"Do"->"MyStyle", "For"->"MyStyle"}],

Cell[StyleData["MyStyle"],
FontColor->RGBColor[0.5, 0, 0.5]]
}


• The thing on the rhs of the rule must be a named style (a slightly archaic and embarrassing limitation in a modern FE, but that's the way it is in v8).
• There's a bug (fixed for future versions) in the validation of this option which can cause a crash if you feed it values formatted in any way other than this.
• This will only work in typeset cells
• This will only work to style things which are lexically word-type tokens. You cannot, for example, auto-style two words in sequence, a subexpression with an operator, or a substring of a word token.
-
Will this code be executed every time I load that notebook? –  rcollyer Mar 6 '12 at 19:44
Based on John's description the answer would be yes providing you are using this newly created stylesheet. –  Mike Honeychurch Mar 6 '12 at 22:20
Thanks John!, @MikeHoneychurch, I don't "think" there's probably anything special about a newly created stylesheet. He probably said it so no other "normal" issues can prevent this from working, since I said that I couldn't make it work. The key was that it only works with typeset cells. Furthermore, the Options Inspector pops up an editor for the values that is TOTALLY misleading, asking a key-password pair and returning the result as a list of pairs. Add this to the bug... –  Rojo Mar 6 '12 at 22:40
@Rojo, yes, the fact that key-password pair should have worked but crashed instead was part of the bug that I fixed (and there were various other issues....this turned out to be a pretty rarely used portion of the code, so it had been busted for a few years w/o anybody noticing). –  John Fultz Mar 9 '12 at 7:59
Rojo is correct that this doesn't have to be in a newly created stylesheet...you could set it in the OI at the notebook or global level, or add it to a shared stylesheet. What I'm doing here is providing a specific formula that any newbie can follow and make work. This, like any other option, works in various and predictable manners which I'm happy to elucidate on in other questions where the issue would be on topic. And, to clarify...it's not "executing code"...it's merely an option setting like Background or FontColor or any other. –  John Fultz Mar 9 '12 at 8:02