1

I have a big org file with lot of topics, and every topic have a books subheading. I would like write a function that can generate a list of all books in org-table. I was trying with (org-map-entries) but no luck so far ;)

enter image description here

1 Answer 1

3

Something like this. Call it with M-x my-org-books-to-table RET while the org buffer with your books is active to insert the table at point:

(defun my-org-books-to-table ()
  "Generate a list of books and insert as org-table."
  (interactive)
  (let ((item "books")
        items)
    ;; Map through all headings with item (text of the headline) "books".
    (org-map-entries
     (lambda ()
       (let ((lvl (number-to-string (1+ (org-outline-level)))))
         ;; Map through children of heading found. Add to books unless its
         ;; heading text matches "books".
         (org-map-entries
          (lambda ()
            (let* ((olp (org-get-outline-path t t))
                   (txt (car (last olp)))
                   (title (nth 0 olp)))
              (push (list title txt) items)))
          (concat "LEVEL=" lvl "+ITEM<>\"" item "\"") 'tree)))
     (concat "ITEM=\"" item "\""))
    (setq items (nreverse items))
    (setq items (append (list '(title topic) 'hline) items))
    (insert (concat (orgtbl-to-orgtbl items nil) "\n"))))
3
  • Very nice. Why is the last nil needed?
    – jagrg
    Commented Oct 26, 2019 at 11:05
  • @jagrg It's not needed, thx. Was a leftover from another go. And also forgot to turn on caching for org-get-outline-path. Modified my answer.
    – Hubisan
    Commented Oct 26, 2019 at 20:48
  • @slk500 updated answer to depend on heading text only and not heading text + level. This still assumes that the title is found at level 1.
    – Hubisan
    Commented Oct 28, 2019 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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