typo
[org-mode.git] / lisp / org-babel-lob.el
blobebb1361691857c0fe5d119b6b8d7952225b502df
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
8 ;; Version: 0.01
10 ;;; License:
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)
15 ;; any later version.
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.
27 ;;; Commentary:
29 ;; See org-babel.org in the parent directory for more information
31 ;;; Code:
32 (require 'org-babel)
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."
42 :group 'org-babel
43 :type 'list)
45 (defun org-babel-lob-ingest (&optional file)
46 "Add all source-blocks defined in FILE to `org-babel-library-of-babel'."
47 (interactive "f")
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)))
51 (when source-name
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")
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
63 the Library."
64 (interactive)
65 (let ((info (org-babel-lob-get-info)))
66 (if 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
79 the word 'call'."
80 (let ((case-fold-search t))
81 (save-excursion
82 (move-beginning-of-line 1)
83 (if (looking-at org-babel-lob-one-liner-regexp)
84 (org-babel-clean-text-properties
85 (format "%s(%s)" (match-string 1) (match-string 2)))))))
87 (defun org-babel-lob-execute (info)
88 (let ((params (org-babel-merge-params
89 org-babel-default-header-args
90 (org-babel-parse-header-arguments (concat ":var results=" info)))))
91 (org-babel-execute-src-block nil (list "emacs-lisp" "results" params))))
93 (provide 'org-babel-lob)
94 ;;; org-babel-lob.el ends here