0

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

2

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"))))
  • Very nice. Why is the last nil needed? – jagrg Oct 26 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 Oct 26 at 20:48
  • Amazing! How to improve this to get all subheading (books names) from heading (books) but without asking about LEVEL? Just to get all books name if heading name is "books" – slk500 Oct 27 at 8:36
  • after replacing it with nill I got this result: | title | topic | |-------+-------------| | lisp | nil | | lisp | book lisp 1 | | lisp | book lisp 2 | | lisp | book lisp 1 | | lisp | book lisp 2 | – slk500 Oct 27 at 18:28
  • does not work either – slk500 Oct 28 at 5:40

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.