EDIT
There is/was a bug in the replace-string-in-region
function.
Using the newest version of that function, your code works alright, except for some unwanted newlines due to the first backslash in the double backslash functioning as an escape character. To fix it, just replace the double backslashes \\
by quadruple backslashes \\\\
END EDIT
It looks like you have encountered a bug in the replace-string-in-region
.
If you closely look at the code of that function then you will find that the end
argument passed to the search-forward
function, does not move while the 'original region' expands (due to replacing a short string with a longer string).
Of course, I did not find this error by reading the source directly, but I was using edebug to investigate the behavior (I strongly advise you to read about edebug, as it is very simple to use, while it is a very powerful tool).
The error you are seeing seems to be triggered because, due to this bug, the position of point
is after the position passed as end
to the search-forward
function.
I guess to make your code work, you would have to fix the replace-string-in-region
to update the position passed as end
to the search-forward
function, or rewrite your algorithm otherwise.
Luckily, it looks like fixing that replace-string-in-region
function should not be too difficult.
It would be great if subsequently you could contribute a patch with the fix (or otherwise create a bug report).