2009-08-05 10:02:25 -04:00
|
|
|
#+title: The Library of Babel
|
2010-06-27 18:50:07 -04:00
|
|
|
#+author: Org-mode People
|
2009-08-05 10:02:25 -04:00
|
|
|
#+STARTUP: odd hideblocks
|
2009-06-12 20:26:28 -04:00
|
|
|
|
2009-08-05 10:02:25 -04:00
|
|
|
* Introduction
|
|
|
|
The Library of Babel is an extensible collection of ready-made and
|
|
|
|
easily-shortcut-callable source-code blocks for handling common
|
|
|
|
tasks. Org-babel comes pre-populated with the source-code blocks
|
|
|
|
located in this file. It is possible to add source-code blocks from
|
2009-08-05 15:35:07 -04:00
|
|
|
any org-mode file to the library by calling =(org-babel-lob-ingest
|
|
|
|
"path/to/file.org")=.
|
2009-09-13 23:15:17 -04:00
|
|
|
|
|
|
|
This file is included in worg mainly less for viewing through the
|
|
|
|
web interface, and more for contribution through the worg git
|
|
|
|
repository. If you have code snippets that you think others may
|
|
|
|
find useful please add them to this file and [[file:~/src/worg/worg-git.org::contribute-to-worg][contribute them]] to
|
|
|
|
worg.
|
|
|
|
|
|
|
|
The raw Org-mode text of this file can be downloaded at
|
|
|
|
[[repofile:contrib/babel/library-of-babel.org][library-of-babel.org]]
|
2009-08-05 10:02:25 -04:00
|
|
|
|
2010-06-10 00:03:21 -04:00
|
|
|
* Simple
|
|
|
|
A collection of simple utility functions
|
|
|
|
|
|
|
|
#+srcname: echo
|
|
|
|
#+begin_src emacs-lisp :var input="echo'd"
|
|
|
|
input
|
|
|
|
#+end_src
|
|
|
|
|
2010-06-06 22:56:09 -04:00
|
|
|
* File I/O
|
|
|
|
** reading and writing files
|
|
|
|
Read the contents of the file at =path= into a string.
|
|
|
|
#+srcname: read
|
|
|
|
#+begin_src emacs-lisp :var path=""
|
|
|
|
(with-temp-filebuffer path
|
|
|
|
(buffer-substring (point-min) (point-max)))
|
|
|
|
#+end_src
|
|
|
|
|
|
|
|
Read the lines of the file at =path= into a list.
|
|
|
|
#+srcname: read-lines
|
|
|
|
#+begin_src emacs-lisp :var path=""
|
|
|
|
(split-string
|
|
|
|
(with-temp-filebuffer path
|
|
|
|
(buffer-substring (point-min) (point-max))))
|
|
|
|
#+end_src
|
|
|
|
|
|
|
|
Write =data= to a file at =path=. If =data= is a list, then write it
|
|
|
|
as a table in traditional Org-mode table syntax.
|
|
|
|
#+srcname: write
|
|
|
|
#+begin_src emacs-lisp :var data="" :var path=""
|
|
|
|
(with-temp-file path
|
|
|
|
(org-babel-insert-result data))
|
|
|
|
nil
|
|
|
|
#+end_src
|
|
|
|
|
2010-06-27 18:50:07 -04:00
|
|
|
** remote files
|
|
|
|
|
|
|
|
Read local or remote file in [[http://www.json.org/][json]] format into emacs-lisp objects.
|
|
|
|
#+srcname: json
|
|
|
|
#+begin_src emacs-lisp :var file='() :var url='()
|
|
|
|
(require 'json)
|
|
|
|
(cond
|
|
|
|
(file
|
|
|
|
(with-temp-filebuffer file
|
|
|
|
(goto-char (point-min))
|
|
|
|
(json-read)))
|
|
|
|
(url
|
|
|
|
(require 'w3m)
|
|
|
|
(with-temp-buffer
|
|
|
|
(w3m-retrieve url)
|
|
|
|
(goto-char (point-min))
|
|
|
|
(json-read))))
|
|
|
|
#+end_src
|
|
|
|
|
2009-06-14 17:10:04 -04:00
|
|
|
* Plotting code
|
2009-08-05 10:02:25 -04:00
|
|
|
|
|
|
|
** R
|
2009-06-14 17:10:04 -04:00
|
|
|
Plot column 2 (y axis) against column 1 (x axis). Columns 3 and beyond, if present, are ignored.
|
2009-06-14 10:54:55 -04:00
|
|
|
|
Enabling LoB to put results in buffer, and slowly moving towards more
unified concept of function calls.
Previously LoB calls were not able to produce results in the
buffer. These changes go some way to allowing them to do that. [There
are still some bugs to deal with]. That meant changing org-babel.el so
that there is a notion of the `source block name' for a LoB line, in
order to construct a #+resname (currently I've made the name the same
as the function call).
I'm also slowly moving towards unifying the notion of `function calls'
a bit more: I've changed the org-babel-lob-one-liner-regexp so that
instead of a monolithic match it now matches first the function name,
and second the function arguments in
parentheses. org-babel-lob-get-info makes that match, and although it
still concatenates them and returns the string, the two elements can
be accessed immediately afterwards using match-string. So that
situation is very similar to org-babel-get-src-block-name, whose
job (in this branch) is also to parse the function *name* and the
function *arguments*. In a few places in the code (esp. function
names), I think the word `info' should be replaced with `call' or
`function call', which I believe more accurately indicates what the
`info' is: a function definition, together with bound
arguments/references.
The function call syntax, i.e. function-name(arg1=ref1), originally
introduced for references (and thereby in LoB), and which I'm
proposing we use throughout, raises the question of default arguments,
and those being over-ridden by supplied arguments, as in e.g. python,
and R.
2009-07-10 22:59:10 -04:00
|
|
|
#+srcname: R-plot(data=R-plot-example-data)
|
|
|
|
#+begin_src R :session *R*
|
2009-06-14 10:54:55 -04:00
|
|
|
plot(data)
|
|
|
|
#+end_src
|
|
|
|
|
Enabling LoB to put results in buffer, and slowly moving towards more
unified concept of function calls.
Previously LoB calls were not able to produce results in the
buffer. These changes go some way to allowing them to do that. [There
are still some bugs to deal with]. That meant changing org-babel.el so
that there is a notion of the `source block name' for a LoB line, in
order to construct a #+resname (currently I've made the name the same
as the function call).
I'm also slowly moving towards unifying the notion of `function calls'
a bit more: I've changed the org-babel-lob-one-liner-regexp so that
instead of a monolithic match it now matches first the function name,
and second the function arguments in
parentheses. org-babel-lob-get-info makes that match, and although it
still concatenates them and returns the string, the two elements can
be accessed immediately afterwards using match-string. So that
situation is very similar to org-babel-get-src-block-name, whose
job (in this branch) is also to parse the function *name* and the
function *arguments*. In a few places in the code (esp. function
names), I think the word `info' should be replaced with `call' or
`function call', which I believe more accurately indicates what the
`info' is: a function definition, together with bound
arguments/references.
The function call syntax, i.e. function-name(arg1=ref1), originally
introduced for references (and thereby in LoB), and which I'm
proposing we use throughout, raises the question of default arguments,
and those being over-ridden by supplied arguments, as in e.g. python,
and R.
2009-07-10 22:59:10 -04:00
|
|
|
#+tblname: R-plot-example-data
|
2009-06-14 10:54:55 -04:00
|
|
|
| 1 | 2 |
|
|
|
|
| 2 | 4 |
|
|
|
|
| 3 | 9 |
|
|
|
|
| 4 | 16 |
|
|
|
|
| 5 | 25 |
|
|
|
|
|
Enabling LoB to put results in buffer, and slowly moving towards more
unified concept of function calls.
Previously LoB calls were not able to produce results in the
buffer. These changes go some way to allowing them to do that. [There
are still some bugs to deal with]. That meant changing org-babel.el so
that there is a notion of the `source block name' for a LoB line, in
order to construct a #+resname (currently I've made the name the same
as the function call).
I'm also slowly moving towards unifying the notion of `function calls'
a bit more: I've changed the org-babel-lob-one-liner-regexp so that
instead of a monolithic match it now matches first the function name,
and second the function arguments in
parentheses. org-babel-lob-get-info makes that match, and although it
still concatenates them and returns the string, the two elements can
be accessed immediately afterwards using match-string. So that
situation is very similar to org-babel-get-src-block-name, whose
job (in this branch) is also to parse the function *name* and the
function *arguments*. In a few places in the code (esp. function
names), I think the word `info' should be replaced with `call' or
`function call', which I believe more accurately indicates what the
`info' is: a function definition, together with bound
arguments/references.
The function call syntax, i.e. function-name(arg1=ref1), originally
introduced for references (and thereby in LoB), and which I'm
proposing we use throughout, raises the question of default arguments,
and those being over-ridden by supplied arguments, as in e.g. python,
and R.
2009-07-10 22:59:10 -04:00
|
|
|
#+lob: R-plot(data=R-plot-example-data)
|
|
|
|
|
2009-07-11 23:59:51 -04:00
|
|
|
#+resname: R-plot(data=R-plot-example-data)
|
2009-07-18 19:11:14 -04:00
|
|
|
: nil
|
Enabling LoB to put results in buffer, and slowly moving towards more
unified concept of function calls.
Previously LoB calls were not able to produce results in the
buffer. These changes go some way to allowing them to do that. [There
are still some bugs to deal with]. That meant changing org-babel.el so
that there is a notion of the `source block name' for a LoB line, in
order to construct a #+resname (currently I've made the name the same
as the function call).
I'm also slowly moving towards unifying the notion of `function calls'
a bit more: I've changed the org-babel-lob-one-liner-regexp so that
instead of a monolithic match it now matches first the function name,
and second the function arguments in
parentheses. org-babel-lob-get-info makes that match, and although it
still concatenates them and returns the string, the two elements can
be accessed immediately afterwards using match-string. So that
situation is very similar to org-babel-get-src-block-name, whose
job (in this branch) is also to parse the function *name* and the
function *arguments*. In a few places in the code (esp. function
names), I think the word `info' should be replaced with `call' or
`function call', which I believe more accurately indicates what the
`info' is: a function definition, together with bound
arguments/references.
The function call syntax, i.e. function-name(arg1=ref1), originally
introduced for references (and thereby in LoB), and which I'm
proposing we use throughout, raises the question of default arguments,
and those being over-ridden by supplied arguments, as in e.g. python,
and R.
2009-07-10 22:59:10 -04:00
|
|
|
|
2009-08-05 10:02:25 -04:00
|
|
|
** Gnuplot
|
|
|
|
|
2010-06-28 13:08:52 -04:00
|
|
|
* Tables
|
|
|
|
** LaTeX Table export
|
2010-06-29 13:10:52 -04:00
|
|
|
*** booktabs
|
|
|
|
This block can be used to wrap a table in the latex =booktabs=
|
|
|
|
environment, it takes the following arguments -- all but the first two
|
|
|
|
are optional.
|
|
|
|
| arg | description |
|
|
|
|
|-------+--------------------------------------------|
|
|
|
|
| table | a reference to the table |
|
|
|
|
| align | optional alignment string |
|
|
|
|
| env | optional environment, default to "tabular" |
|
|
|
|
| width | optional width specification string |
|
|
|
|
|
|
|
|
#+srcname: booktabs
|
|
|
|
#+begin_src emacs-lisp :var table='((:head) hline (:body)) :var align='() :var env="tabular" :var width='() :noweb yes :results latex
|
|
|
|
(flet ((to-tab (tab)
|
|
|
|
(orgtbl-to-generic
|
|
|
|
(mapcar (lambda (lis)
|
|
|
|
(if (listp lis)
|
|
|
|
(mapcar (lambda (el)
|
|
|
|
(if (stringp el)
|
|
|
|
el
|
|
|
|
(format "%S" el))) lis)
|
|
|
|
lis)) tab)
|
|
|
|
(list :lend " \\\\" :sep " & " :hline "\\hline"))))
|
|
|
|
(org-fill-template
|
|
|
|
"
|
|
|
|
\\begin{%env}%width%align
|
|
|
|
\\toprule
|
|
|
|
%table
|
|
|
|
\\bottomrule
|
|
|
|
\\end{%env}\n"
|
|
|
|
(list
|
|
|
|
(cons "env" (or env "table"))
|
|
|
|
(cons "width" (if width (format "{%s}" width) ""))
|
|
|
|
(cons "align" (if align (format "{%s}" align) ""))
|
|
|
|
(cons "table"
|
|
|
|
;; only use \midrule if it looks like there are column headers
|
|
|
|
(if (equal 'hline (second table))
|
|
|
|
(concat (to-tab (list (first table)))
|
|
|
|
"\n\\midrule\n"
|
|
|
|
(to-tab (cddr table)))
|
|
|
|
(to-tab table))))))
|
|
|
|
#+end_src
|
|
|
|
|
|
|
|
*** longtable
|
2010-06-28 13:08:52 -04:00
|
|
|
This block can be used to wrap a table in the latex =longtable=
|
2010-06-28 16:23:48 -04:00
|
|
|
environment, it takes the following arguments -- all but the first two
|
|
|
|
are optional.
|
2010-06-28 13:08:52 -04:00
|
|
|
| arg | description |
|
|
|
|
|-----------+-------------------------------------------------------------|
|
|
|
|
| table | a reference to the table |
|
|
|
|
| align | optional alignment string |
|
|
|
|
| width | optional width specification string |
|
|
|
|
| hline | the string to use as hline separator, defaults to "\\hline" |
|
|
|
|
| head | optional "head" string |
|
|
|
|
| firsthead | optional "firsthead" string |
|
|
|
|
| foot | optional "foot" string |
|
|
|
|
| lastfoot | optional "lastfoot" string |
|
|
|
|
|
2010-06-28 16:23:48 -04:00
|
|
|
#+srcname: longtable
|
2010-06-29 13:10:52 -04:00
|
|
|
#+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
|
2010-06-28 13:08:52 -04:00
|
|
|
(org-fill-template
|
|
|
|
"
|
2010-06-28 16:23:48 -04:00
|
|
|
\\begin{longtable}%width%align
|
2010-06-28 13:08:52 -04:00
|
|
|
%firsthead
|
|
|
|
%head
|
|
|
|
%foot
|
|
|
|
%lastfoot
|
|
|
|
|
|
|
|
%table
|
2010-06-28 16:23:48 -04:00
|
|
|
\\end{longtable}\n"
|
2010-06-28 13:08:52 -04:00
|
|
|
(list
|
|
|
|
(cons "width" (if width (format "{%s}" width) ""))
|
|
|
|
(cons "align" (if align (format "{%s}" align) ""))
|
|
|
|
(cons "firsthead" (if firsthead (concat firsthead "\n\\endfirsthead\n") ""))
|
|
|
|
(cons "head" (if head (concat head "\n\\endhead\n") ""))
|
|
|
|
(cons "foot" (if foot (concat foot "\n\\endfoot\n") ""))
|
|
|
|
(cons "lastfoot" (if lastfoot (concat lastfoot "\n\\endlastfoot\n") ""))
|
|
|
|
(cons "table" (orgtbl-to-generic
|
|
|
|
(mapcar (lambda (lis)
|
|
|
|
(if (listp lis)
|
|
|
|
(mapcar (lambda (el)
|
|
|
|
(if (stringp el)
|
|
|
|
el
|
|
|
|
(format "%S" el))) lis)
|
|
|
|
lis)) table)
|
|
|
|
(list :lend " \\\\" :sep " & " :hline hline)))))
|
|
|
|
#+end_src
|
2009-08-18 16:07:19 -04:00
|
|
|
|
2010-06-28 13:08:52 -04:00
|
|
|
** Elegant lisp for transposing a matrix.
|
2009-08-18 16:07:19 -04:00
|
|
|
|
|
|
|
#+tblname: transpose-example
|
|
|
|
| 1 | 2 | 3 |
|
|
|
|
| 4 | 5 | 6 |
|
|
|
|
|
|
|
|
#+srcname: transpose
|
|
|
|
#+begin_src emacs-lisp :var table=transpose-example
|
|
|
|
(apply #'mapcar* #'list table)
|
|
|
|
#+end_src
|
|
|
|
|
|
|
|
#+resname:
|
|
|
|
| 1 | 4 |
|
|
|
|
| 2 | 5 |
|
|
|
|
| 3 | 6 |
|
|
|
|
|
2009-08-05 10:02:25 -04:00
|
|
|
* Misc
|
2009-07-11 23:59:51 -04:00
|
|
|
#+srcname: python-identity(a=1)
|
Enabling LoB to put results in buffer, and slowly moving towards more
unified concept of function calls.
Previously LoB calls were not able to produce results in the
buffer. These changes go some way to allowing them to do that. [There
are still some bugs to deal with]. That meant changing org-babel.el so
that there is a notion of the `source block name' for a LoB line, in
order to construct a #+resname (currently I've made the name the same
as the function call).
I'm also slowly moving towards unifying the notion of `function calls'
a bit more: I've changed the org-babel-lob-one-liner-regexp so that
instead of a monolithic match it now matches first the function name,
and second the function arguments in
parentheses. org-babel-lob-get-info makes that match, and although it
still concatenates them and returns the string, the two elements can
be accessed immediately afterwards using match-string. So that
situation is very similar to org-babel-get-src-block-name, whose
job (in this branch) is also to parse the function *name* and the
function *arguments*. In a few places in the code (esp. function
names), I think the word `info' should be replaced with `call' or
`function call', which I believe more accurately indicates what the
`info' is: a function definition, together with bound
arguments/references.
The function call syntax, i.e. function-name(arg1=ref1), originally
introduced for references (and thereby in LoB), and which I'm
proposing we use throughout, raises the question of default arguments,
and those being over-ridden by supplied arguments, as in e.g. python,
and R.
2009-07-10 22:59:10 -04:00
|
|
|
#+begin_src python
|
|
|
|
a
|
|
|
|
#+end_src
|
2009-07-11 23:59:51 -04:00
|
|
|
|
|
|
|
#+srcname: python-add(a=1, b=2)
|
|
|
|
#+begin_src python
|
|
|
|
a + b
|
|
|
|
#+end_src
|
|
|
|
|
|
|
|
|
|
|
|
|