(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.
This commit is contained in:
Stefan Monnier 2025-04-16 10:15:16 -04:00
parent bf737dc42a
commit 4532fbefec
2 changed files with 8 additions and 5 deletions

View file

@ -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

View file

@ -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)));