Auto highlighting input cells depending on input expression (eg its Head)
There are several ways to do that. Perhaps, the simplest is to do something like this:
ClearAll[color];
SetAttributes[color, HoldAll];
color[_Export | _Import] := LightGreen;
color[_Set] := LightBlue;
color[_] := None;
and then
$Pre =
Function[
code
,
SetOptions[ EvaluationCell[], Background -> color[code]]; code
,
HoldAll
];
An alternative to using $Pre
as suggested by @LeonidShifrin is to use the Cell
option CellProlog
in a style sheet. Here is an example style sheet that does this:
ss = Notebook[
{
Cell[StyleData[StyleDefinitions->"Default.nb"]],
Cell[StyleData["Input"],
CellProlog :> Module[{cell = NotebookRead[EvaluationCell[]], held, color},
held = Replace[cell, Cell[b_, __] :> ToExpression[b, StandardForm, Hold]];
held = Replace[held, Hold[b_;] :> Hold[b]];
color = Replace[held,
{
Hold[_Set] -> LightBlue,
Hold[_Import | _Export] -> LightGreen,
_ -> None
}
];
CurrentValue[EvaluationCell[], Background] = color
]
]
},
StyleDefinitions->"PrivateStylesheetFormatting.nb"
];
To use, evaluate:
SetOptions[EvaluationNotebook[], StyleDefinitions -> ss];
and then any Input cells you evaluate should turn color if they match your criteria.