PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/babel/library-of-babel.org

https://github.com/justinabrahms/org-mode
Org | 282 lines | 247 code | 35 blank | 0 comment | 9 complexity | 55d82d68e16cc41080e003107d5cc6b0 MD5 | raw file
  1. #+title: The Library of Babel
  2. #+author: Org-mode People
  3. #+STARTUP: odd hideblocks
  4. * Introduction
  5. The Library of Babel is an extensible collection of ready-made and
  6. easily-shortcut-callable source-code blocks for handling common
  7. tasks. Org-babel comes pre-populated with the source-code blocks
  8. located in this file. It is possible to add source-code blocks from
  9. any org-mode file to the library by calling =(org-babel-lob-ingest
  10. "path/to/file.org")=.
  11. This file is included in worg mainly less for viewing through the
  12. web interface, and more for contribution through the worg git
  13. repository. If you have code snippets that you think others may
  14. find useful please add them to this file and [[file:~/src/worg/worg-git.org::contribute-to-worg][contribute them]] to
  15. worg.
  16. The raw Org-mode text of this file can be downloaded at
  17. [[repofile:contrib/babel/library-of-babel.org][library-of-babel.org]]
  18. * Simple
  19. A collection of simple utility functions
  20. #+srcname: echo
  21. #+begin_src emacs-lisp :var input="echo'd"
  22. input
  23. #+end_src
  24. * File I/O
  25. ** reading and writing files
  26. Read the contents of the file at =file=. The =:results vector= and
  27. =:results scalar= header arguments can be used to read the contents of
  28. file as either a table or a string.
  29. #+srcname: read
  30. #+begin_src emacs-lisp :var file="" :var format=""
  31. (if (string= format "csv")
  32. (with-temp-buffer
  33. (org-table-import (expand-file-name file) nil)
  34. (org-table-to-lisp))
  35. (with-temp-buffer
  36. (insert-file-contents (expand-file-name file))
  37. (buffer-string)))
  38. #+end_src
  39. Write =data= to a file at =file=. If =data= is a list, then write it
  40. as a table in traditional Org-mode table syntax.
  41. #+srcname: write
  42. #+begin_src emacs-lisp :var data="" :var file="" :var ext='()
  43. (flet ((echo (r) (if (stringp r) r (format "%S" r))))
  44. (with-temp-file file
  45. (case (and (listp data)
  46. (or ext (intern (file-name-extension file))))
  47. ('tsv (insert (orgtbl-to-tsv data '(:fmt echo))))
  48. ('csv (insert (orgtbl-to-csv data '(:fmt echo))))
  49. (t (org-babel-insert-result data)))))
  50. nil
  51. #+end_src
  52. ** remote files
  53. **** json
  54. Read local or remote file in [[http://www.json.org/][json]] format into emacs-lisp objects.
  55. #+srcname: json
  56. #+begin_src emacs-lisp :var file='() :var url='()
  57. (require 'json)
  58. (cond
  59. (file
  60. (with-temp-filebuffer file
  61. (goto-char (point-min))
  62. (json-read)))
  63. (url
  64. (require 'w3m)
  65. (with-temp-buffer
  66. (w3m-retrieve url)
  67. (goto-char (point-min))
  68. (json-read))))
  69. #+end_src
  70. **** Google docs
  71. The following code blocks make use of the [[http://code.google.com/p/googlecl/][googlecl]] Google command line
  72. tool. This tool provides functionality for accessing Google services
  73. from the command line, and the following code blocks use /googlecl/
  74. for reading from and writing to Google docs with Org-mode code blocks.
  75. ****** read a document from Google docs
  76. The =google= command seems to be throwing "Moved Temporarily" errors
  77. when trying to download textual documents, but this is working fine
  78. for spreadsheets.
  79. #+source: gdoc-read
  80. #+begin_src emacs-lisp :var title="example" :var format="csv"
  81. (let* ((file (concat title "." format))
  82. (cmd (format "google docs get --format %S --title %S" format title)))
  83. (message cmd) (message (shell-command-to-string cmd))
  84. (prog1 (if (string= format "csv")
  85. (with-temp-buffer
  86. (org-table-import (shell-quote-argument file) '(4))
  87. (org-table-to-lisp))
  88. (with-temp-buffer
  89. (insert-file-contents (shell-quote-argument file))
  90. (buffer-string)))
  91. (delete-file file)))
  92. #+end_src
  93. For example, a line like the following can be used to read the
  94. contents of a spreadsheet named =num-cells= into a table.
  95. : #+call: gdoc-read(title="num-cells"")
  96. A line like the following can be used to read the contents of a
  97. document as a string.
  98. : #+call: gdoc-read(title="loremi", :format "txt")
  99. ****** write a document to a Google docs
  100. Write =data= to a google document named =title=. If =data= is tabular
  101. it will be saved to a spreadsheet, otherwise it will be saved as a
  102. normal document.
  103. #+source: gdoc-write
  104. #+begin_src emacs-lisp :var title="babel-upload" :var data=fibs(n=10) :results silent
  105. (let* ((format (if (listp data) "csv" "txt"))
  106. (tmp-file (make-temp-file "org-babel-google-doc" nil (concat "." format)))
  107. (cmd (format "google docs upload --title %S %S" title tmp-file)))
  108. (with-temp-file tmp-file
  109. (insert
  110. (if (listp data)
  111. (orgtbl-to-csv
  112. data '(:fmt (lambda (el) (if (stringp el) el (format "%S" el)))))
  113. (if (stringp data) data (format "%S" data)))))
  114. (message cmd)
  115. (prog1 (shell-command-to-string cmd) (delete-file tmp-file)))
  116. #+end_src
  117. example usage
  118. : #+source: fibs
  119. : #+begin_src emacs-lisp :var n=8
  120. : (flet ((fib (m) (if (< m 2) 1 (+ (fib (- m 1)) (fib (- m 2))))))
  121. : (mapcar (lambda (el) (list el (fib el))) (number-sequence 0 (- n 1))))
  122. : #+end_src
  123. :
  124. : #+call: gdoc-write(title="fibs", data=fibs(n=10))
  125. * Plotting code
  126. ** R
  127. Plot column 2 (y axis) against column 1 (x axis). Columns 3 and beyond, if present, are ignored.
  128. #+srcname: R-plot(data=R-plot-example-data)
  129. #+begin_src R
  130. plot(data)
  131. #+end_src
  132. #+tblname: R-plot-example-data
  133. | 1 | 2 |
  134. | 2 | 4 |
  135. | 3 | 9 |
  136. | 4 | 16 |
  137. | 5 | 25 |
  138. #+lob: R-plot(data=R-plot-example-data)
  139. #+resname: R-plot(data=R-plot-example-data)
  140. : nil
  141. ** Gnuplot
  142. * Tables
  143. ** LaTeX Table export
  144. *** booktabs
  145. This block can be used to wrap a table in the latex =booktabs=
  146. environment, it takes the following arguments -- all but the first two
  147. are optional.
  148. | arg | description |
  149. |-------+--------------------------------------------|
  150. | table | a reference to the table |
  151. | align | optional alignment string |
  152. | env | optional environment, default to "tabular" |
  153. | width | optional width specification string |
  154. #+srcname: booktabs
  155. #+begin_src emacs-lisp :var table='((:head) hline (:body)) :var align='() :var env="tabular" :var width='() :noweb yes :results latex
  156. (flet ((to-tab (tab)
  157. (orgtbl-to-generic
  158. (mapcar (lambda (lis)
  159. (if (listp lis)
  160. (mapcar (lambda (el)
  161. (if (stringp el)
  162. el
  163. (format "%S" el))) lis)
  164. lis)) tab)
  165. (list :lend " \\\\" :sep " & " :hline "\\hline"))))
  166. (org-fill-template
  167. "
  168. \\begin{%env}%width%align
  169. \\toprule
  170. %table
  171. \\bottomrule
  172. \\end{%env}\n"
  173. (list
  174. (cons "env" (or env "table"))
  175. (cons "width" (if width (format "{%s}" width) ""))
  176. (cons "align" (if align (format "{%s}" align) ""))
  177. (cons "table"
  178. ;; only use \midrule if it looks like there are column headers
  179. (if (equal 'hline (second table))
  180. (concat (to-tab (list (first table)))
  181. "\n\\midrule\n"
  182. (to-tab (cddr table)))
  183. (to-tab table))))))
  184. #+end_src
  185. *** longtable
  186. This block can be used to wrap a table in the latex =longtable=
  187. environment, it takes the following arguments -- all but the first two
  188. are optional.
  189. | arg | description |
  190. |-----------+-------------------------------------------------------------|
  191. | table | a reference to the table |
  192. | align | optional alignment string |
  193. | width | optional width specification string |
  194. | hline | the string to use as hline separator, defaults to "\\hline" |
  195. | head | optional "head" string |
  196. | firsthead | optional "firsthead" string |
  197. | foot | optional "foot" string |
  198. | lastfoot | optional "lastfoot" string |
  199. #+srcname: longtable
  200. #+begin_src emacs-lisp :var table='((:table)) :var align='() :var width='() :var hline="\\hline" :var firsthead='() :var head='() :var foot='() :var lastfoot='() :noweb yes :results latex
  201. (org-fill-template
  202. "
  203. \\begin{longtable}%width%align
  204. %firsthead
  205. %head
  206. %foot
  207. %lastfoot
  208. %table
  209. \\end{longtable}\n"
  210. (list
  211. (cons "width" (if width (format "{%s}" width) ""))
  212. (cons "align" (if align (format "{%s}" align) ""))
  213. (cons "firsthead" (if firsthead (concat firsthead "\n\\endfirsthead\n") ""))
  214. (cons "head" (if head (concat head "\n\\endhead\n") ""))
  215. (cons "foot" (if foot (concat foot "\n\\endfoot\n") ""))
  216. (cons "lastfoot" (if lastfoot (concat lastfoot "\n\\endlastfoot\n") ""))
  217. (cons "table" (orgtbl-to-generic
  218. (mapcar (lambda (lis)
  219. (if (listp lis)
  220. (mapcar (lambda (el)
  221. (if (stringp el)
  222. el
  223. (format "%S" el))) lis)
  224. lis)) table)
  225. (list :lend " \\\\" :sep " & " :hline hline)))))
  226. #+end_src
  227. ** Elegant lisp for transposing a matrix.
  228. #+tblname: transpose-example
  229. | 1 | 2 | 3 |
  230. | 4 | 5 | 6 |
  231. #+srcname: transpose
  232. #+begin_src emacs-lisp :var table=transpose-example
  233. (apply #'mapcar* #'list table)
  234. #+end_src
  235. #+resname:
  236. | 1 | 4 |
  237. | 2 | 5 |
  238. | 3 | 6 |
  239. * Misc
  240. #+srcname: python-identity(a=1)
  241. #+begin_src python
  242. a
  243. #+end_src
  244. #+srcname: python-add(a=1, b=2)
  245. #+begin_src python
  246. a + b
  247. #+end_src