Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zealinux/7874ffdec890c31d7d3c26991fd97125 to your computer and use it in GitHub Desktop.
Save zealinux/7874ffdec890c31d7d3c26991fd97125 to your computer and use it in GitHub Desktop.
org-save-org-structure-as-dir-file-skeleton to save the org structure as dirs and files (marked with tag :file:)
(defun org-interpret-org-as-dir-file-skeleton ()
"Return the outline paths as lists.
Separated in those headlines not tagged 'file' and those tagged with 'file'."
(let (output-files output-dirs)
(org-map-entries
(lambda ()
(let* ((headline (car (cddddr (org-heading-components))))
(headline-path
(reverse
(cons
headline (reverse (org-get-outline-path))))))
(cond
((member "file" (org-get-local-tags))
(setf output-files (cons headline-path output-files)))
(t
(setf output-dirs (cons headline-path output-dirs)))))))
(list output-dirs output-files)))
(defun org-save-org-structure-as-dir-file-skeleton ()
"Write a directory/file structure to disk according to the Org structure.
Every headline gets interpreted as directory except when it's
tagged with 'file'.
This function expects an org structure which can logically be
mapped to a filesystem. E.g. no dirs within files may occur."
(interactive)
(let ((dirs-and-files (org-interpret-org-as-dir-file-skeleton)))
(mapc
#'make-directory
(mapcar
(lambda (x) (reduce (lambda (x y) (concat x "/" y)) x))
(sort (car dirs-and-files) (lambda (x y) (< (length x) (length y))))))
(mapc
(lambda (x) (save-excursion
(save-buffer (set-buffer (find-file-noselect x t)))))
(mapcar
(lambda (x) (reduce (lambda (x y) (concat x "/" y)) x))
(cadr dirs-and-files)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment