6
$\begingroup$

Make cursor stay in input cell provides a function for keeping the cursor in a cell after it was evaluated. I tried adding that function directly into KeyEventTranslations.tr to create a keyboard shortcut but that led to Mathematica sending an error when loading. I tried wrapping it in KernelExecute but that did not work. I would also like to modify shift plus enter to remove the setting from the function in the link when I want the standard way of evaluating a cell.

$\endgroup$

1 Answer 1

10
+100
$\begingroup$

The following adds a new menu item "Evaluate Cells (Keep Selection)" with shortcut "Alt+Shift+Return" that keeps the cursor where it is:

FrontEndExecute@FrontEnd`AddMenuCommands[
    If[$OperatingSystem == "Windows", "EvaluateCells", "HandleShiftReturn"], {
      MenuItem["Evaluate Cells (Keep Selection)",
        KernelExecute@evaluateKeepSelection[],
        System`MenuKey[
          "Return",
          System`Modifiers -> {"Shift", "Option"}
          ],
        System`MenuEvaluator -> Automatic
        ]}]
 
evaluateKeepSelection[] := 
 With[{nb := EvaluationNotebook[], c := EvaluationCell[]}, 
  With[{cv := 
     CurrentValue[nb, {"TaggingRules", "LastCursorPosition"}], 
    pos := FrontEndExecute@
      FrontEnd`UndocumentedGetSelectionPacket[nb]}, 
   cv = "CharacterRange" /. pos;
   FrontEndExecute@FrontEndToken["EvaluateCells"]; 
   SetOptions[nb, 
    CellEpilog :> 
     If[ListQ[cv], SelectionMove[c, Before, CellContents]; 
      SelectionMove[nb, Next, Character, First[cv]]; 
      SelectionMove[nb, After, Character]; 
      FrontEndExecute@
       Table[FrontEndToken["SelectNext"], -Subtract @@ cv];
      cv = False]]]
  ]

enter image description here

Note that this keeps the usual behavior of "Shift+Return" as it is, it just adds an additional shortcut that keeps the selection.

$\endgroup$
9
  • $\begingroup$ I do not see a menu item either on Linux with version 13.2. Maybe I could use resources.wolframcloud.com/FunctionRepository/resources/… somehow to add the menu item ? (I did not try using that function i just saw it in the past) $\endgroup$ Commented Apr 16, 2023 at 14:13
  • $\begingroup$ @MichaelE2 Can you try again? I tweaked the call to AddMenuCommands slightly, hopefully that makes it more robust... $\endgroup$
    – Lukas Lang
    Commented Apr 16, 2023 at 14:38
  • $\begingroup$ @userrandrand That resource function does nothing else than call FrontEndExecute@FrontEnd`AddMenuCommands, so I am a bit surprised my code didn't work for you (or MichaelE2). As I already mentioned, I tweaked the call slightly in the hopes to make the call work now $\endgroup$
    – Lukas Lang
    Commented Apr 16, 2023 at 14:39
  • $\begingroup$ @MichaelE2 Looks like the relevant token is "HandleShiftReturn" instead of "EvaluateCells" on Linux for some reason (I assume it's the same on macOS as well). I updated the answer to include a check for the OS, and it works now for a fresh install on Ubuntu for me. Can you check again on macOS? $\endgroup$
    – Lukas Lang
    Commented Apr 16, 2023 at 16:21
  • $\begingroup$ Success! It works. (+1) $\endgroup$
    – Michael E2
    Commented Apr 16, 2023 at 18:12

Your Answer

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

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