瀏覽代碼

org-element: Implement `org-element-create'

* lisp/org-element.el (org-element-create): New function.
(org-element-adopt-elements): Small refactoring.
Nicolas Goaziou 9 年之前
父節點
當前提交
7ee2d93122
共有 1 個文件被更改,包括 19 次插入12 次删除
  1. 19 12
      lisp/org-element.el

+ 19 - 12
lisp/org-element.el

@@ -489,18 +489,18 @@ objects, or a strings.
 
 The function takes care of setting `:parent' property for CHILD.
 Return parent element."
-  ;; Link every child to PARENT. If PARENT is nil, it is a secondary
-  ;; string: parent is the list itself.
-  (mapc (lambda (child)
-	  (org-element-put-property child :parent (or parent children)))
-	children)
-  ;; Add CHILDREN at the end of PARENT contents.
-  (when parent
-    (apply 'org-element-set-contents
-	   parent
-	   (nconc (org-element-contents parent) children)))
-  ;; Return modified PARENT element.
-  (or parent children))
+  (if (not children) parent
+    ;; Link every child to PARENT. If PARENT is nil, it is a secondary
+    ;; string: parent is the list itself.
+    (dolist (child children)
+      (org-element-put-property child :parent (or parent children)))
+    ;; Add CHILDREN at the end of PARENT contents.
+    (when parent
+      (apply #'org-element-set-contents
+	     parent
+	     (nconc (org-element-contents parent) children)))
+    ;; Return modified PARENT element.
+    (or parent children)))
 
 (defun org-element-extract-element (element)
   "Extract ELEMENT from parse tree.
@@ -573,6 +573,13 @@ The function takes care of setting `:parent' property for NEW."
     ;; Transfer type.
     (setcar old (car new))))
 
+(defun org-element-create (type &optional props &rest children)
+  "Create a new element of type TYPE.
+Optional argument PROPS, when non-nil, is a plist defining the
+properties of the element.  CHILDREN can be elements, objects or
+strings."
+  (apply #'org-element-adopt-elements (list type props) children))
+
 (defun org-element-copy (datum)
   "Return a copy of DATUM.
 DATUM is an element, object, string or nil.  `:parent' property