Undo and cursor position

Sure, and it does so when you apply the step programatically too, but it computes it from the old selection, so that doesn’t give you the selection you’re looking for (the cursor will probalby simply stay at the start of the document).

Your code is using a post-step position and applying it before the step, which is probably not what you mean and might produce an error if the position is out of range. You might get better results using oldEnd.