Sometimes, having all double brackets [[...]]
transformed into \\[LeftDoubleBracket] ... \\[RightDoubleBracket]
can really facilitate code reading.
On a given notebook, how to transform all occurrences automatically (on input cells)?
The other way around is easier, but a couple of buttons interface will get extra... thanks.
Answer
Palette button
CreatePalette[
Button["Compact Part",
Do[
With[{
celldata = NotebookRead[cell],
pattern = RuleDelayed[
RowBox[{h__, "[", RowBox[{"[", spec___, "]"}], "]"}],
RowBox[{h, "\[LeftDoubleBracket]", RowBox[{spec}],
"\[RightDoubleBracket]"}]
]
},
If[
Not @ FreeQ[celldata, First @ pattern],
SelectionMove[cell, Cell, All, AutoScroll -> False];
NotebookWrite[
InputNotebook[],
ReplaceRepeated[celldata, pattern], AutoScroll -> False
];
]
];
,
{cell, Cells[InputNotebook[], CellStyle -> "Input"]}
];
MessageDialog["Done"];
]]
It may happen that it will miss an example from time to time, it should be rare though. In heavily edited notebook I have faced box structures that were not "proper" but good enough for FrontEnd to understand. So it may happen this "proper" pattern won't catch them.
Comments
Post a Comment