How to execute this code on startup?
Here's a stylesheet version that mimics Kuba's code:
With[{cv := CurrentValue[EvaluationCell[], {TaggingRules, "LastCursorPosition"}]},
SetOptions[EvaluationNotebook[],
StyleDefinitions -> Notebook[
{
Cell[StyleData[StyleDefinitions->"Default.nb"]],
Cell[StyleData["Input"],
CellEventActions -> {
{"MenuCommand","HandleShiftReturn"} :> Set[
cv,
Replace[
Lookup[
Developer`CellInformation @ EvaluationCell[],
"CursorPosition"
],
{
{_, i_} -> i,
_ -> False
}
]
],
PassEventsDown->True
},
CellEpilog :> Replace[cv,
i_Integer :> (
SelectionMove[EvaluationCell[], Before, CellContents];
SelectionMove[EvaluationNotebook[], Next, Character, i];
cv = False
)
],
TaggingRules -> {}
]
},
StyleDefinitions->"PrivateStylesheetFormatting.nb"
]
]
]
You can modify the default stylesheet to use for new notebooks, see the answers to How to change the default Notebook stylesheet.
For future generations, this is how you do it. Place this in your init.m
:
If[$Notebooks,
AppendTo[CurrentValue[$FrontEnd, {CodeAssistOptions, "IncludedCompletions"}], "Blacklisted"];
Begin["CursorSave`"];
SaveCursorPosition[] := (
$LastNotebookObject = EvaluationNotebook[];
$LastCellObject = First @ SelectedCells[$LastNotebookObject];
$LastCellCursorPosition = First @ Lookup[
Developer`CellInformation[EvaluationCell[]],
"CursorPosition"
];
);
RestoreCursorPosition[] := (
SelectionMove[$LastCellObject, Before, CellContents];
SelectionMove[$LastNotebookObject, Next, Character, $LastCellCursorPosition];
);
SetOptions[$FrontEndSession, FrontEndEventActions -> {{"MenuCommand", "HandleShiftReturn"} :>
(SaveCursorPosition[]; FrontEndTokenExecute["EvaluateCells"]; RestoreCursorPosition[])}];
End[];
];