I have a function that removes any surrounding [] {} or () from the current selection of the notebook given by NotebookObject[]
.
NotebookWrite[#, Replace[NotebookRead[#], {
RowBox[{"(", x_, ")"}] :> x,
RowBox[{"{", x_, "}"}] :> x,
RowBox[{"[", x_, "]"}] :> x
}], All] & @ NotebookObject[]
This works in a notebook. It would be handy to have as a keybinding. I have struggled to add it to MenuSetup.tr or KeyEventTranslations.tr. For example:
MenuItem["Desurround",
FrontEndExecute[{
FrontEnd`NotebookWrite[FrontEnd`SelectedNotebook[],
Replace[FrontEnd`NotebookRead[FrontEnd`SelectedNotebook[]], {
RowBox[{"(", x_, ")"}] :> x,
RowBox[{"{", x_, "}"}] :> x,
RowBox[{"[", x_, "]"}] :> x
}], All] }],
MenuKey["Backspace", Modifiers->{"Control"}]
]
simply has no noticeable effect in the front end. I've tried several combinations of appending FrontEnd`
onto every function to no avail. FrontEndExecute gets scared of Head
s that arn't "menu command"s such that an error is thrown for things like FrontEndExecute[Module[...]]
or FrontEndExecute[CombinedExpression[...]]
. I've gotten close by using FrontEndExecute
with FrontEndApply
to wrap my function around the selection but the logic gets complicated and messy (the strings and box forms do not play nice) as far as I can tell.
Is a way to have a nontrivial function like the above attached to a keybinding. Are there any examples of MenuSetup.tr files that I can use to study this sort of thing? How can I take the above function and add its functionality to a keybinding?
Replace
you need toKernelExecute
I guess. p.s. you may want to try DevTools`LiveActions $\endgroup$ – Kuba♦ Oct 6 '20 at 8:36