]> git.eshelyaron.com Git - emacs.git/commitdiff
* test/src/editfns-tests.el (editfns--replace-region): New test
authorStefan Monnier <monnier@iro.umontreal.ca>
Fri, 28 Feb 2025 22:10:56 +0000 (17:10 -0500)
committerEshel Yaron <me@eshelyaron.com>
Tue, 4 Mar 2025 20:56:08 +0000 (21:56 +0100)
This test fails, sadly, because `replace-buffer-contents` is not
careful enough to something like `replace_range`.

(cherry picked from commit 1cfbbcfaf657e767ed5743565b62eeecde3a4ef5)

src/editfns.c
test/src/editfns-tests.el

index 3dff49fb00cc465914703f6c8fc4d84bd9462ceb..12700527ef370e80d1d0972abc2689643bfd92fc 100644 (file)
@@ -2050,6 +2050,7 @@ nil.  */)
 
   if (early_abort)
     {
+      /* FIXME: Use 'replace_range'!  */
       del_range (min_a, ZV);
       Finsert_buffer_substring (source, Qnil,Qnil);
       SAFE_FREE_UNBIND_TO (count, Qnil);
@@ -2099,6 +2100,7 @@ nil.  */)
           eassert (beg_a <= end_a);
           eassert (beg_b <= end_b);
           eassert (beg_a < end_a || beg_b < end_b);
+          /* FIXME: Use 'replace_range'!  */
           if (beg_a < end_a)
             del_range (beg_a, end_a);
           if (beg_b < end_b)
index 29b7a850838773f6ce1c61ec66d6e38678f08af4..09af179a1805a2389c28e9118b6d5120d550eed7 100644 (file)
   (should (equal (buffer-substring-no-properties (point-min) (point-max))
                  (concat (string (char-from-name "SMILE")) "1234"))))
 
+(defun editfns--replace-region (from to string)
+  (save-excursion
+    (save-restriction
+      (narrow-to-region from to)
+      (let ((buf (current-buffer)))
+        (with-temp-buffer
+          (let ((str-buf (current-buffer)))
+            (insert string)
+            (with-current-buffer buf
+              (replace-buffer-contents str-buf))))))))
+
+(ert-deftest editfns-tests--replace-region ()
+  :expected-result :failed
+  (with-temp-buffer
+    (insert "here is some text")
+    (let ((m5n (copy-marker (+ (point-min) 5)))
+          (m5a (copy-marker (+ (point-min) 5) t))
+          (m6n (copy-marker (+ (point-min) 6)))
+          (m6a (copy-marker (+ (point-min) 6) t))
+          (m7n (copy-marker (+ (point-min) 7)))
+          (m7a (copy-marker (+ (point-min) 7) t)))
+      (editfns--replace-region (+ (point-min) 5) (+ (point-min) 7) "be")
+      (should (equal (buffer-string) "here be some text"))
+      (should (equal (point) (point-max)))
+      ;; Markers before the replaced text stay before.
+      (should (= m5n (+ (point-min) 5)))
+      (should (= m5a (+ (point-min) 5)))
+      ;; Markers in the replaced text can end up at either end, depending
+      ;; on whether they're advance-after-insert or not.
+      (should (= m6n (+ (point-min) 5)))
+      (should (<= (+ (point-min) 5) m6a (+ (point-min) 7)))
+      ;; Markers after the replaced text stay after.
+      (should (= m7n (+ (point-min) 7)))
+      (should (= m7a (+ (point-min) 7))))))
+
 (ert-deftest delete-region-undo-markers-1 ()
   "Make sure we don't end up with freed markers reachable from Lisp."
   ;; https://debbugs.gnu.org/cgi/bugreport.cgi?bug=30931#40