Even in your example, in step 5, restoring the caret position with sci_set_selection_start/sci_set_selection_end/sci_set_selection and will automatically restore the last X again, if you use my modifications. Typically this should actually be achieved by sci_set_target_start and sci_set_target_end. A lot of selection functions already alter the last X, for example `sci_replace_sel`.