This is trickier than I thought. The primary reason of the bug is that record_insert combines two adjacent insertions thus messing up my comparison after format decoding wrt the start of the inserted region. Unless someone has a better idea, I'd handle this by inserting an undo boundary for `insert-file-contents'. The second problem is that `insert-file-contents' does not consider an insertion a buffer change when the buffer is empty. This is obviously TRT in the file-visiting case, but doesn't seem quite right in the non-visiting case. Both issues are involved in the buggy behaviors observed. Please try another time with the attached patch (I'm afraid it won't be the last).