From 95c305490e0be46f8be13b0b88a1989feee65508 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Thu, 25 Oct 2012 21:33:57 +0200 Subject: [PATCH] org-element: Add :parent text property to strings in parse tree * lisp/org-element.el (org-element-property): Access to text properties when argument is a string. (org-element-put-property): Correctly set property when target is a string. (org-element-adopt-elements): Also put :parent properties on strings. * testing/lisp/test-org-element.el: Add test. --- lisp/org-element.el | 16 +++++++++------- testing/lisp/test-org-element.el | 6 +++++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lisp/org-element.el b/lisp/org-element.el index a4a9ae682..db6a2db02 100644 --- a/lisp/org-element.el +++ b/lisp/org-element.el @@ -75,6 +75,9 @@ ;; and elements containing objects will also have `:contents-begin' ;; and `:contents-end' properties to delimit contents. ;; +;; At the lowest level, a `:parent' property is also attached to any +;; string, as a text property. +;; ;; Lisp-wise, an element or an object can be represented as a list. ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where: ;; TYPE is a symbol describing the Org element or object. @@ -376,7 +379,8 @@ It can also return the following special value: (defsubst org-element-property (property element) "Extract the value from the PROPERTY of an ELEMENT." - (plist-get (nth 1 element) property)) + (if (stringp element) (get-text-property 0 property element) + (plist-get (nth 1 element) property))) (defsubst org-element-contents (element) "Extract contents from an ELEMENT." @@ -392,9 +396,9 @@ element or object type." (defsubst org-element-put-property (element property value) "In ELEMENT set PROPERTY to VALUE. Return modified element." - (when (consp element) - (setcar (cdr element) (plist-put (nth 1 element) property value))) - element) + (if (stringp element) (org-add-props element nil property value) + (setcar (cdr element) (plist-put (nth 1 element) property value)) + element)) (defsubst org-element-set-contents (element &rest contents) "Set ELEMENT contents to CONTENTS. @@ -430,9 +434,7 @@ The function takes care of setting `:parent' property for CHILD. Return parent element." (if (not parent) children ;; Link every child to PARENT. - (mapc (lambda (child) - (unless (stringp child) - (org-element-put-property child :parent parent))) + (mapc (lambda (child) (org-element-put-property child :parent parent)) children) ;; Add CHILDREN at the end of PARENT contents. (apply 'org-element-set-contents diff --git a/testing/lisp/test-org-element.el b/testing/lisp/test-org-element.el index 3c4ef81b7..70a263dcd 100644 --- a/testing/lisp/test-org-element.el +++ b/testing/lisp/test-org-element.el @@ -76,12 +76,16 @@ Some other text (ert-deftest test-org-element/put-property () "Test `org-element-put-property' specifications." + ;; Standard test. (org-test-with-temp-text "* Headline\n *a*" (let ((tree (org-element-parse-buffer))) (org-element-put-property (org-element-map tree 'bold 'identity nil t) :test 1) (should (org-element-property - :test (org-element-map tree 'bold 'identity nil t)))))) + :test (org-element-map tree 'bold 'identity nil t))))) + ;; Put property on a string. + (should + (org-element-property :test (org-element-put-property "Paragraph" :test t)))) (ert-deftest test-org-element/set-contents () "Test `org-element-set-contents' specifications." -- 2.11.4.GIT