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

3.2 KiB

The Library of Babel

<div id="logo" style="float: left; text-align: center; max-width: 340px; font-size: 8pt; margin-left: 1em;"> <p> <img src="../../images/babel/library-of-babel.png" alt="Library of Babel"/> <div id="attr"> The Library of Babel, by Pierre Clayette <p> <a href="http://downlode.org/Etext/library_of_babel.html">Full text of the Borges short story</a> </p> </div> </p> </div>

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 any org-mode file to the library by calling (org-babel-lob-ingest "path/to/file.org").

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 contribute them to worg.

The raw Org-mode text of this file can be downloaded at library-of-babel.org

Simple

A collection of simple utility functions

  input

File I/O

reading and writing files

Read the contents of the file at path into a string.

  (with-temp-filebuffer path
    (buffer-substring (point-min) (point-max)))

Read the lines of the file at path into a list.

  (split-string
   (with-temp-filebuffer path
     (buffer-substring (point-min) (point-max))))

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

  (with-temp-file path
    (org-babel-insert-result data))
  nil

Plotting code

R

Plot column 2 (y axis) against column 1 (x axis). Columns 3 and beyond, if present, are ignored.

plot(data)
1 2
2 4
3 9
4 16
5 25
nil

Gnuplot

Table/Matrix manipulation

Elegant lisp code for transposing a matrix.

1 2 3
4 5 6
  (apply #'mapcar* #'list table)
1 4
2 5
3 6

Misc

a
a + b