Browse Source

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.
Nicolas Goaziou 5 years ago
parent
commit
95c305490e
2 changed files with 14 additions and 8 deletions
  1. 9 7
      lisp/org-element.el
  2. 5 1
      testing/lisp/test-org-element.el

+ 9 - 7
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

+ 5 - 1
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."