3
$\begingroup$

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 Heads 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?

$\endgroup$
1
  • $\begingroup$ Since you are using Replace you need to KernelExecute I guess. p.s. you may want to try DevTools`LiveActions $\endgroup$ – Kuba Oct 6 '20 at 8:36
2
$\begingroup$

Thanks to Kuba for telling me about DevTools`. It is a very neat package. But I did not want to use Ctrl + ' and then Backspace. But the KernelExecute hint helped me find 16165 which showed how to add a command to a shortcut. The jist is, put your function/command in a .m file (no formatting, exactly how it appears in a notebook) then use this syntax in an appropriate location in the MenuSetup.tr file

MenuItem[
    "Desurround",
    KernelExecute @ Get["/path/to/the/file.m"],
    MenuKey["Backspace", Modifiers -> {"Control", "Shift"}],
    MenuEvaluator -> Automatic
],

For more information on MenuSetup.tr and KeyEvenTranslation.tr see 5212 and 197177 for a more up to date post on the topic. It also looks like b3m2a1's answer to 145847 has a package that can help automate some of this stuff.

P.S. Heres an upgrade to the function that handles quotes "" as well. Here is what I ultimately put in the .m file

NotebookWrite[#, Replace[NotebookRead[#], {
    x_String :> StringJoin[Characters[x][[2 ;; -2]]] /; 
        First @ Characters @ x === "\"" && Last @ Characters @ x === "\"",
    RowBox[{a_String, x__, b_String}] :> 
        RowBox[{
            StringJoin[Characters[a] ~ Drop ~ 1], 
            x, 
            StringJoin[Characters[b] ~ Drop ~ -1]
        }] /; 
        StringMatchQ[a, "\"" ~~ __] && StringMatchQ[b, __ ~~ "\""],
    RowBox[{"(", x_, ")"}] :> x,
    RowBox[{"{", x_, "}"}] :> x,
    RowBox[{"[", x_, "]"}] :> x,
    RowBox[{"(", x__, ")"}] :> RowBox[{x}],
    RowBox[{"{", x__, "}"}] :> RowBox[{x}],
    RowBox[{"[", x__, "]"}] :> RowBox[{x}]
}], All] & @ SelectedNotebook[]
$\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.