9
$\begingroup$

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.

$\endgroup$
2
  • 2
    $\begingroup$ the lefts are easier :) $\endgroup$ Oct 29, 2015 at 17:18
  • $\begingroup$ Cell>Convert To>StandardForm does this (it is what i use), but it also makes other changes that may not be desirable $\endgroup$ Oct 29, 2015 at 22:37

1 Answer 1

5
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ I tried it on ref/Part and it works. $\endgroup$
    – Kuba
    Oct 29, 2015 at 18:44
  • $\begingroup$ Tried it on a 700 pages notebook, and, although it seems to work, the end message doesn't appear. When I have a little time, I will try to trace if it is the notebook size or some specific code content. $\endgroup$
    – P. Fonseca
    Oct 31, 2015 at 20:49
  • $\begingroup$ @P.Fonseca it may be that's the size :) $\endgroup$
    – Kuba
    Oct 31, 2015 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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