]> git.eshelyaron.com Git - emacs.git/commitdiff
(Freplace_region_contents): Treat point as insert-before marker
authorStefan Monnier <monnier@iro.umontreal.ca>
Wed, 16 Apr 2025 14:15:16 +0000 (10:15 -0400)
committerEshel Yaron <me@eshelyaron.com>
Thu, 17 Apr 2025 07:08:28 +0000 (09:08 +0200)
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.

(cherry picked from commit 4532fbefece210061d01ab9523f3054aadb1c45a)

src/editfns.c
src/insdel.c

index 227f5f0be2cfa2f756fc7bbafb1790de805d114a..a938d09453489163aab6519a0d82f012f4d3349a 100644 (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
index 6656221faa1b317dbaa00ccf75471281efdec515..7e361fbb2cea8c3bac1ecee6926527edd97e7a5b 100644 (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)));