Given an org document, I'd like to traverse all the nodes (headlines) that have the following property ARTICLE_TYPE, and extract the body (excluding the drawers) for each one.

I'm struggling to find the appropriate org-element-map expression that will give me this.

Any ideas?


I think this does what you want. It does assume there is no nesting of the headlines.

#+BEGIN_SRC emacs-lisp :results code
(org-element-map (org-element-parse-buffer)
  (lambda (hl)
    (when (org-element-property :ARTICLE_TYPE hl) 
     (goto-char (org-element-property :begin hl))
       (org-element-property :contents-end hl)))))
  • Apparently this returns only the result for the first headline that matches (no idea why it doesn't return all). (Rant: Generally, for a package as good as org mode, I'm a bit shocked at how poor/nonexistent the API is. I would have expected a way to traverse all headlines in whatever order I like (e.g. BFS vs DFS), as well as things like "Return all children of this node" and so on). (BTW, learned a lot from your blog in the past - thanks!) – Beetle B. Dec 19 '18 at 0:13
  • For me, this returns all the nodes. org-element-map has an optional argument that stops at the first match, but I don't use it in this example. – John Kitchin Dec 19 '18 at 1:18
  • Marking as correct, as it seems to work for you. Probably something wrong on my setup and I haven't had time to debug. – Beetle B. Dec 22 '18 at 0:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.