org-element-insert-before: Fix return value when setting plain-text

* lisp/org-element-ast.el (org-element-set-element): When setting
plain-text node, return the new node instead of immutable old one.
* testing/lisp/test-org-element.el (test-org-element/set): Add test.
This commit is contained in:
Ihor Radchenko 2024-02-29 11:43:52 +03:00
parent 755fef38f5
commit 286a8fb798
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
2 changed files with 14 additions and 2 deletions

View File

@ -1131,8 +1131,11 @@ The function takes care of setting `:parent' property for NEW."
(eq new-type 'plain-text))
;; We cannot replace OLD with NEW since strings are not mutable.
;; We take the long path.
(progn (org-element-insert-before new old)
(org-element-extract old))
(progn
(org-element-insert-before new old)
(org-element-extract old)
;; We will return OLD.
(setq old new))
;; Since OLD is going to be changed into NEW by side-effect, first
;; make sure that every element or object within NEW has OLD as
;; parent.

View File

@ -755,6 +755,15 @@ Some other text
(text (org-element-map tree 'plain-text 'identity nil t)))
(org-element-set text "b")
(org-element-map tree 'plain-text 'identity nil t)))))
;; Replace string inside anonymous element with another string.
(let* ((parent (org-element-create 'anonymous nil "test"))
(str (car (org-element-contents parent))))
(let ((return (org-element-set str "repl"))
(new (car (org-element-contents parent))))
;; Return the modified value.
(should (eq return new))
(should (equal new "repl"))
(should (eq (org-element-parent new) parent))))
;; KEEP-PROPS
(should
(org-element-property