Library of Babel (org-mode)

This is my org-mode Library of Babel.

File I/O

Reading and writing files

Read the contents of the file at file. The :results vector and :results scalar header arguments can be used to read the contents of file as either a table or a string.

(if (string= format "csv")
    (with-temp-buffer
      (org-table-import (expand-file-name file) nil)
      (org-table-to-lisp))
  (with-temp-buffer
    (insert-file-contents (expand-file-name file))
    (buffer-string)))

Write data to a file at file. If data is a list, then write it as a table in traditional Org-mode table syntax.

(flet ((echo (r) (if (stringp r) r (format "%S" r))))
  (with-temp-file file
    (case (and (listp data)
               (or ext (intern (file-name-extension file))))
      ('tsv (insert (orgtbl-to-tsv data '(:fmt echo))))
      ('csv (insert (orgtbl-to-csv data '(:fmt echo))))
      (t    (org-babel-insert-result data)))))
nil