9
$\begingroup$

When you type ->, it is automatically turned into the \[Rule] character. Can we configure our frontends to do the same for the [[ and ]] characters, which could be turned into \[LeftDoubleBracket] and \[RightDoubleBracket]?

You can easily do this manually by typing <esc>[[<esc>, but I would like to do this automatically.

This question was originally asked by me in the chat and has answers there; this question is so that those answers can be added and kept for the community.

$\endgroup$
  • 3
    $\begingroup$ Do you really want that when writing func[func2[arg]]? I changed KeyEventTranslations.tr to have <kbd>Alt gr</kbd>+<kbd>i</kbd>/<kbd>o</kbd>. $\endgroup$ – Coolwater Apr 7 at 19:04
  • $\begingroup$ Yes, this is part of the problem. Ideally it would only happen after you have typed the matching [[, but more than likely it needs to be done at shift-enter at the earliest. For [[ the problem is simpler because there is no time when you would type [[ except for as part of a Part expression. $\endgroup$ – Carl Lange Apr 7 at 19:05
  • 1
    $\begingroup$ closely related: 98175 $\endgroup$ – Kuba Apr 7 at 20:50
  • 1
    $\begingroup$ Related using KeyEventTranslations 5221 $\endgroup$ – Rohit Namjoshi Apr 7 at 21:41
  • $\begingroup$ Unless @Kuba or another mod overrules, I will leave this open as I am asking how to do it as-I-work rather than afterwards, but the questions are certainly strongly related. $\endgroup$ – Carl Lange Apr 7 at 21:42
9
$\begingroup$

You can use CellEpilog to modify the contents of the cell upon evaluation:

SetOptions[EvaluationNotebook[],
 CellEpilog :> If[
   ! TrueQ[
     Max@CurrentValue[CellChangeTimes] <= 
       CurrentValue[EvaluationCell[], {TaggingRules, "autoreplaceTime"}]
     ],
   CurrentValue[EvaluationCell[], {TaggingRules, "autoreplaceTime"}] = 
     Max@CurrentValue[CellChangeTimes];
   NotebookWrite[
    EvaluationCell[],
    Replace[NotebookRead[EvaluationCell[]],
     RowBox@{head_, "[", RowBox@{"[", part_, "]"}, "]"} :>
      RowBox@{head, "\[LeftDoubleBracket]", part, "\[RightDoubleBracket]"}, All]
    ]
   ]
 ]

Demonstration: https://i.stack.imgur.com/4V5WJ.gif

This could of course be extended to do similar replacements for <|…|> etc. To minimize the performance hit, we keep track of the last CellChangeTimes value and only rerun the replacement code when it changed.

The nice thing about an approach like this (i.e. manipulation of the cell's box structure) is that it's rather straightforward to identify the bracket pairs to replace, since the front-end does all the necessary parsing for us.

An alternative to putting the code into CellEpilog might be to create a menu item for it, one could also modify the "Evaluate cells" menu item to run this code on all selected cells first.

$\endgroup$
9
$\begingroup$

Here is a stylesheet solution modeled after Lucas' answer, except that I use CellEventActions to tie the transformation to a keyboard short cut:

SetOptions[
    EvaluationNotebook[],
    StyleDefinitions -> Notebook[{
        Cell[StyleData[StyleDefinitions->"Default.nb"]],
        Cell[StyleData["Input"],
            CellEventActions -> {
                {"KeyDown","y"} :> If[CurrentValue["ControlKey"],
                    NotebookWrite[
                        EvaluationCell[],
                        With[{read=NotebookRead[EvaluationCell[]]},
                            Cell[
                                ReplaceAll[
                                    First[read],
                                    RuleDelayed[
                                        RowBox[{head_,"[",RowBox[{"[",part_,"]"}],"]"}],
                                        RowBox[{head,"\[LeftDoubleBracket]",part,"\[RightDoubleBracket]"}]
                                    ]
                                ],
                                Sequence@@Rest[read]
                            ]
                        ]
                    ];
                    SelectionMove[EvaluationNotebook[],Before,CellContents];
                    SelectionMove[EvaluationNotebook[],After,CellContents]
                ],
                PassEventsDown->True
            }]
        },
        StyleDefinitions->"PrivateStylesheetFormatting.nb"
    ]
]

Using the short cut ctrl + y will convert [[ and ]] to the double bracketing bar versions.

$\endgroup$
5
$\begingroup$

It's maybe not exactly what you want but you can always select the cell, expression, or part of it ( Ctrl-. is helpful for this ) and then do Ctrl-Shift-n to change the selection to Standard Form. This includes the changes to [[ and ]] and doesn't require any set up or customisation.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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