summaryrefslogtreecommitdiff
path: root/test/src/editfns-tests.el
diff options
context:
space:
mode:
authorStefan Monnier <monnier@iro.umontreal.ca>2025-03-28 00:46:53 -0400
committerStefan Monnier <monnier@iro.umontreal.ca>2025-03-29 17:49:05 -0400
commit7c82cc8b975175aebbad1c43ec1cd98b3232f482 (patch)
tree2f2f5ac19ec7055442da0bd58507314d213b9bdd /test/src/editfns-tests.el
parentf60fc1287d499e8c93857b1b96e8bd2467b22c8d (diff)
downloademacs-7c82cc8b975175aebbad1c43ec1cd98b3232f482.tar.gz
emacs-7c82cc8b975175aebbad1c43ec1cd98b3232f482.tar.bz2
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/editfns-tests.el')
-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 @@
(narrow-to-region 8 13)
(goto-char 12)
(should (looking-at " \\'"))
- (replace-buffer-contents source)
+ (replace-region-contents (point-min) (point-max) source)
(should (looking-at " \\'")))
(should (equal (marker-buffer marker) (current-buffer)))
(should (equal (marker-position marker) 16)))
@@ -306,7 +306,7 @@
(let ((source (current-buffer)))
(with-temp-buffer
(insert "foo BAR baz qux")
- (replace-buffer-contents source)
+ (replace-region-contents (point-min) (point-max) source)
(should (equal-including-properties
(buffer-string)
"foo bar baz qux"))))))
@@ -318,44 +318,44 @@
(switch-to-buffer "b")
(insert-char (char-from-name "SMILE"))
(insert "5678")
- (replace-buffer-contents "a")
+ (replace-region-contents (point-min) (point-max) (get-buffer "a"))
(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))))))
+ (let ((tmpbuf (current-buffer)))
+ (insert " be ")
+ (narrow-to-region (+ (point-min) 2) (- (point-max) 2))
+ (dolist (args `((,tmpbuf)
+ (,(vector tmpbuf (point-min) (point-max)))
+ (,"be")
+ (,(vector tmpbuf (point-min) (point-max)) 0)
+ (,"be" 0)))
+ (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)))
+ (apply #'replace-region-contents
+ (+ (point-min) 5) (+ (point-min) 7) args)
+ (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)))))
+ (widen)))))
(ert-deftest delete-region-undo-markers-1 ()
"Make sure we don't end up with freed markers reachable from Lisp."