From f93cc661c656c7a74c67fd0299662d1604b2b243 Mon Sep 17 00:00:00 2001 From: Ihor Radchenko Date: Fri, 3 Mar 2023 16:31:37 +0100 Subject: [PATCH] org-element-set-contents: Do alter the anonymous parents * lisp/org-element.el (org-element-set-contents): Do alter anonymous elements (el1 el2 ...). Such elements are used, for example, when parsing keyword values during export, like :title. * testing/lisp/test-org-element.el (test-org-element/set-contents): Add test. The patch fixed bug during export when exporting a subtree with option stat:nil. The :title during subtree export is taken from the heading title and parsed. However, the parsed value is stored outside the parse tree, in :title property of the INFO channel. The parsed value does get filtered through `org-export--prune-tree', but before this commit, `org-element-set-contents' did not actually alter the out-of-AST-tree parent lists of elements. Reported-by: Leo Butler Link: https://orgmode.org/list/87mt4w8epo.fsf@t14.reltub.ca --- lisp/org-element.el | 9 ++++++++- testing/lisp/test-org-element.el | 8 +++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lisp/org-element.el b/lisp/org-element.el index d7847a678..f8442511c 100644 --- a/lisp/org-element.el +++ b/lisp/org-element.el @@ -548,7 +548,14 @@ Return modified element." "Set ELEMENT's contents to CONTENTS. Return ELEMENT." (cond ((null element) contents) - ((not (symbolp (car element))) contents) + ((not (symbolp (car element))) + (if (not (listp element)) + ;; Non-element. + contents + ;; Anonymous element (el1 el2 ...) + (setcar element (car contents)) + (setcdr element (cdr contents)) + element)) ((cdr element) (setcdr (cdr element) contents) element) (t (nconc element contents)))) diff --git a/testing/lisp/test-org-element.el b/testing/lisp/test-org-element.el index 43f1d860f..957e99194 100644 --- a/testing/lisp/test-org-element.el +++ b/testing/lisp/test-org-element.el @@ -125,7 +125,13 @@ Some other text (org-test-with-temp-text "* Headline\n *a*" (let ((tree (org-element-parse-buffer))) (org-element-set-contents (org-element-map tree 'bold 'identity nil t)) - (org-element-contents (org-element-map tree 'bold 'identity nil t)))))) + (org-element-contents (org-element-map tree 'bold 'identity nil t))))) + ;; Set contents of anonymous elements. + (should + (equal '#1=((b (:parent #1#))) + (let ((element '#1=((a (:parent #1#)) (b (:parent #1#))))) + (org-element-set-contents element `(b (:parent ,element))) + element)))) (ert-deftest test-org-element/secondary-p () "Test `org-element-secondary-p' specifications."