1 ;;; org-babel-lob.el --- The Library of Babel: off-the-shelf functions for data analysis and plotting using org-babel
3 ;; Copyright (C) 2009 Dan Davison, Eric Schulte
5 ;; Author: Dan Davison, Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; See org-babel.org in the parent directory for more information
33 (require 'org-babel-table
)
35 (defvar org-babel-library-of-babel nil
36 "Library of source-code blocks. This is an association list.
37 Populate the library by adding files to `org-babel-lob-files'.")
39 (defcustom org-babel-lob-files
'()
40 "Files used to populate the `org-babel-library-of-babel'. To
41 add files to this list use the `org-babel-lob-ingest' command."
45 (defun org-babel-lob-ingest (&optional file
)
46 "Add all source-blocks defined in FILE to `org-babel-library-of-babel'."
48 (org-babel-map-source-blocks file
49 (let ((source-name (intern (org-babel-get-src-block-name)))
50 (info (org-babel-get-src-block-info)))
52 (setq org-babel-library-of-babel
53 (cons (cons source-name info
)
54 (assq-delete-all source-name org-babel-library-of-babel
)))))))
56 ;; functions for executing lob one-liners
58 (defvar org-babel-lob-one-liner-regexp
"#\\+lob:[ \t]+\\([^\(\)\n]+\\)\(\\([^\n]*\\)\)[ \t]*\\([^\n]*\\)\n")
60 (defun org-babel-lob-execute-maybe ()
61 "Detect if this is context for a org-babel Library Of Babel
62 src-block and if so then run the appropriate source block from
65 (let ((info (org-babel-lob-get-info)))
66 (if (first info
) (progn (org-babel-lob-execute info
) t
) nil
)))
68 (add-hook 'org-ctrl-c-ctrl-c-hook
'org-babel-lob-execute-maybe
)
70 (defun org-babel-lob-get-info ()
71 "Return the function call supplied on the current Library of
72 Babel line as a string.
74 This function is analogous to org-babel-get-src-block-name. For
75 both functions, after they are called, (match-string 1) matches
76 the function name, and (match-string 2) matches the function
77 arguments inside the parentheses. I think perhaps these functions
78 should be renamed to bring out this similarity, perhaps involving
80 (let ((case-fold-search t
))
82 (move-beginning-of-line 1)
83 (if (looking-at org-babel-lob-one-liner-regexp
)
84 (mapcar #'org-babel-clean-text-properties
85 (list (format "%s(%s)" (match-string 1) (match-string 2))
86 (match-string 3)))))))
88 (defun org-babel-lob-execute (info)
89 (let ((params (org-babel-merge-params
90 org-babel-default-header-args
91 (org-babel-parse-header-arguments
92 (org-babel-clean-text-properties
93 (concat ":var results=" (mapconcat #'identity info
" ")))))))
94 (org-babel-execute-src-block nil
(list "emacs-lisp" "results" params
))))
96 (provide 'org-babel-lob
)
97 ;;; org-babel-lob.el ends here