aboutsummaryrefslogtreecommitdiffstats
path: root/test/src
diff options
context:
space:
mode:
authorStefan Monnier2025-03-28 00:46:53 -0400
committerStefan Monnier2025-03-29 17:49:05 -0400
commit7c82cc8b975175aebbad1c43ec1cd98b3232f482 (patch)
tree2f2f5ac19ec7055442da0bd58507314d213b9bdd /test/src
parentf60fc1287d499e8c93857b1b96e8bd2467b22c8d (diff)
downloademacs-7c82cc8b975175aebbad1c43ec1cd98b3232f482.tar.gz
emacs-7c82cc8b975175aebbad1c43ec1cd98b3232f482.zip
(replace-region-contents): Improve and promote (bug#76313)
Swap the role of `replace-region-contents` and `replace-buffer-contents`, so `replace-region-contents` is the main function, implemented in C, and `replace-buffer-contents` is a mere wrapper (marked as obsolete). Also remove the need to rely on narrowing and on describing the new text as a function. Finally, allow MAX-SECS==0 to require a cheap replacement, and add an INHERIT argument. * src/editfns.c: Include `coding.h`. (Freplace_region_contents): Rename from `Freplace_buffer_contents`. Change calling convention to that of `replace-region-contents`. Add more options for the SOURCE argument. Add INHERIT argument. Skip the costly algorithm if MAX-SECS is 0. * src/insdel.c (replace_range): Allow NEW to be a buffer. * lisp/subr.el (replace-buffer-contents): New implementation. * lisp/emacs-lisp/subr-x.el (replace-region-contents): Delete. * doc/lispref/text.texi (Replacing): Document new API for `replace-region-contents`. Remove documentation of `replace-buffer-contents`. * test/src/editfns-tests.el (replace-buffer-contents-1) (replace-buffer-contents-2, replace-buffer-contents-bug31837): Use `replace-region-contents`. (editfns--replace-region): Delete. (editfns-tests--replace-region): Use `replace-region-contents`. Adds tests for new types of SOURCE args.
Diffstat (limited to 'test/src')
-rw-r--r--test/src/editfns-tests.el68
1 files changed, 34 insertions, 34 deletions
diff --git a/test/src/editfns-tests.el b/test/src/editfns-tests.el
index c3f825c6149..3da9d4e8acd 100644
--- a/test/src/editfns-tests.el
+++ b/test/src/editfns-tests.el
@@ -289,7 +289,7 @@
289 (narrow-to-region 8 13) 289 (narrow-to-region 8 13)
290 (goto-char 12) 290 (goto-char 12)
291 (should (looking-at " \\'")) 291 (should (looking-at " \\'"))
292 (replace-buffer-contents source) 292 (replace-region-contents (point-min) (point-max) source)
293 (should (looking-at " \\'"))) 293 (should (looking-at " \\'")))
294 (should (equal (marker-buffer marker) (current-buffer))) 294 (should (equal (marker-buffer marker) (current-buffer)))
295 (should (equal (marker-position marker) 16))) 295 (should (equal (marker-position marker) 16)))
@@ -306,7 +306,7 @@
306 (let ((source (current-buffer))) 306 (let ((source (current-buffer)))
307 (with-temp-buffer 307 (with-temp-buffer
308 (insert "foo BAR baz qux") 308 (insert "foo BAR baz qux")
309 (replace-buffer-contents source) 309 (replace-region-contents (point-min) (point-max) source)
310 (should (equal-including-properties 310 (should (equal-including-properties
311 (buffer-string) 311 (buffer-string)
312 "foo bar baz qux")))))) 312 "foo bar baz qux"))))))
@@ -318,44 +318,44 @@
318 (switch-to-buffer "b") 318 (switch-to-buffer "b")
319 (insert-char (char-from-name "SMILE")) 319 (insert-char (char-from-name "SMILE"))
320 (insert "5678") 320 (insert "5678")
321 (replace-buffer-contents "a") 321 (replace-region-contents (point-min) (point-max) (get-buffer "a"))
322 (should (equal (buffer-substring-no-properties (point-min) (point-max)) 322 (should (equal (buffer-substring-no-properties (point-min) (point-max))
323 (concat (string (char-from-name "SMILE")) "1234")))) 323 (concat (string (char-from-name "SMILE")) "1234"))))
324 324
325(defun editfns--replace-region (from to string)
326 (save-excursion
327 (save-restriction
328 (narrow-to-region from to)
329 (let ((buf (current-buffer)))
330 (with-temp-buffer
331 (let ((str-buf (current-buffer)))
332 (insert string)
333 (with-current-buffer buf
334 (replace-buffer-contents str-buf))))))))
335
336(ert-deftest editfns-tests--replace-region () 325(ert-deftest editfns-tests--replace-region ()
337 ;; :expected-result :failed 326 ;; :expected-result :failed
338 (with-temp-buffer 327 (with-temp-buffer
339 (insert "here is some text") 328 (let ((tmpbuf (current-buffer)))
340 (let ((m5n (copy-marker (+ (point-min) 5))) 329 (insert " be ")
341 (m5a (copy-marker (+ (point-min) 5) t)) 330 (narrow-to-region (+ (point-min) 2) (- (point-max) 2))
342 (m6n (copy-marker (+ (point-min) 6))) 331 (dolist (args `((,tmpbuf)
343 (m6a (copy-marker (+ (point-min) 6) t)) 332 (,(vector tmpbuf (point-min) (point-max)))
344 (m7n (copy-marker (+ (point-min) 7))) 333 (,"be")
345 (m7a (copy-marker (+ (point-min) 7) t))) 334 (,(vector tmpbuf (point-min) (point-max)) 0)
346 (editfns--replace-region (+ (point-min) 5) (+ (point-min) 7) "be") 335 (,"be" 0)))
347 (should (equal (buffer-string) "here be some text")) 336 (with-temp-buffer
348 (should (equal (point) (point-max))) 337 (insert "here is some text")
349 ;; Markers before the replaced text stay before. 338 (let ((m5n (copy-marker (+ (point-min) 5)))
350 (should (= m5n (+ (point-min) 5))) 339 (m5a (copy-marker (+ (point-min) 5) t))
351 (should (= m5a (+ (point-min) 5))) 340 (m6n (copy-marker (+ (point-min) 6)))
352 ;; Markers in the replaced text can end up at either end, depending 341 (m6a (copy-marker (+ (point-min) 6) t))
353 ;; on whether they're advance-after-insert or not. 342 (m7n (copy-marker (+ (point-min) 7)))
354 (should (= m6n (+ (point-min) 5))) 343 (m7a (copy-marker (+ (point-min) 7) t)))
355 (should (<= (+ (point-min) 5) m6a (+ (point-min) 7))) 344 (apply #'replace-region-contents
356 ;; Markers after the replaced text stay after. 345 (+ (point-min) 5) (+ (point-min) 7) args)
357 (should (= m7n (+ (point-min) 7))) 346 (should (equal (buffer-string) "here be some text"))
358 (should (= m7a (+ (point-min) 7)))))) 347 (should (equal (point) (point-max)))
348 ;; Markers before the replaced text stay before.
349 (should (= m5n (+ (point-min) 5)))
350 (should (= m5a (+ (point-min) 5)))
351 ;; Markers in the replaced text can end up at either end, depending
352 ;; on whether they're advance-after-insert or not.
353 (should (= m6n (+ (point-min) 5)))
354 (should (<= (+ (point-min) 5) m6a (+ (point-min) 7)))
355 ;; Markers after the replaced text stay after.
356 (should (= m7n (+ (point-min) 7)))
357 (should (= m7a (+ (point-min) 7)))))
358 (widen)))))
359 359
360(ert-deftest delete-region-undo-markers-1 () 360(ert-deftest delete-region-undo-markers-1 ()
361 "Make sure we don't end up with freed markers reachable from Lisp." 361 "Make sure we don't end up with freed markers reachable from Lisp."