;;; Filesystem Outline Generator

(defun fo-tree-output-to-org ()
  "Converts the depth representation of the `tree' command to stars.
Point in the current buffer should be at the start of the output."
  (save-excursion
    (replace-regexp "    ?" "*"))
  (save-excursion
    (replace-regexp "\\(|\\|`?--\\)" ""))
  (replace-regexp "^" "*")
  (delete-char 1))

(defun filesystem-outline (root)
  "Generates an Org Mode file from the output of `tree ROOT'."
  (interactive "DRoot directory: ")
  (let* ((root (file-name-as-directory root))
         (name (file-name-nondirectory (substring root 0 -1)))
         (buffer (generate-new-buffer (format "*Outline of %s*" name))))
    (set-buffer buffer)
    (insert "-*- mode: org -*-")
    (center-line)
    (insert "\n")
    (shell-command (format "tree %s -a --noreport --dirsfirst" root) t)
    (goto-char (point-min))
    (save-excursion
      (next-line 1)
      (let ((kill-whole-line t))
        (kill-line))
      (fo-tree-output-to-org))
    (switch-to-buffer buffer)
    (org-mode)))