Sorry, the above process threw some warnings. I didn't notice them before:

Warning (emacs): Encountered (#<marker at 1 in *scratch*> . -14) entry in undo list with no matching (TEXT . POS) entry



On 8 March 2017 at 23:38, nitish chandra <nitishchandrachinta@gmail.com> wrote:

Only marker originally pointing inside the deleted region were recorded
here, and this is to make sure they regain their previous position after
the reinsertion.

I removed this part of the code to see what changes.

1. I put a marker in a line
2. Deleted the line
3. Undo-ed the delete

The marker is still in the proper place. So markers in the deleted string do seem to regain their position properly.