From 4532fbefece210061d01ab9523f3054aadb1c45a Mon Sep 17 00:00:00 2001 From: Stefan Monnier Date: Wed, 16 Apr 2025 10:15:16 -0400 Subject: [PATCH] (Freplace_region_contents): Treat point as insert-before marker Experience suggests that it's more often useful to keep point at the end of the replacement than it is to keep point at the beginning of the replacement. This also aligns the behavior of `replace-region-contents` with that of `insert`. * src/insdel.c (replace_range): Treat PT like an insert-before marker. * src/editfns.c (Freplace_region_contents): Adjust docstring accordingly. --- src/editfns.c | 9 +++++---- src/insdel.c | 4 +++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/editfns.c b/src/editfns.c index 227f5f0be2c..a938d094534 100644 --- a/src/editfns.c +++ b/src/editfns.c @@ -1928,10 +1928,11 @@ If optional argument INHERIT is non-nil, the inserted text will inherit properties from adjoining text. As far as possible the replacement is non-destructive, i.e. existing -buffer contents, markers, properties, and overlays in the current -buffer stay intact. However, if point is at the end of the replaced -text, it may not be at the end of the replacement when this function -returns. +buffer contents, markers, point, properties, and overlays in the current +buffer stay intact. Point is treated like an "insert before" marker: +if point starts at END, it will always be at the end of the replacement +when this function returns, whereas if point starts at BEG it will +remain at BEG only if the replaced text is not empty. Because this function can be very slow if there is a large number of differences between the two buffers, there are two optional arguments diff --git a/src/insdel.c b/src/insdel.c index 6656221faa1..7e361fbb2ce 100644 --- a/src/insdel.c +++ b/src/insdel.c @@ -1637,7 +1637,9 @@ replace_range (ptrdiff_t from, ptrdiff_t to, Lisp_Object new, #endif /* Relocate point as if it were a marker. */ - if (from < PT) + if (from < PT + /* Mimic 'insert' when FROM==TO==PT). */ + || PT == to) adjust_point ((from + inschars - min (PT, to)), (from_byte + outgoing_insbytes - min (PT_BYTE, to_byte)));