TODO allow tangle to be called on a single source block
[org-mode.git] / lisp / org-babel-table.el
blob04ff35ce662335d9a0e0eee0964977bdf5994895
1 ;;; org-babel-table.el --- integration for calling org-babel functions from tables
3 ;; Copyright (C) 2009 Eric Schulte
5 ;; Author: 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 ;; Should allow calling functions from org-mode tables using the
30 ;; function `sbe' as so...
31 ;;
32 ;; #+begin_src emacs-lisp :results silent
33 ;; (defun fibbd (n) (if (< n 2) 1 (+ (fibbd (- n 1)) (fibbd (- n 2)))))
34 ;; #+end_src
35 ;;
36 ;; #+srcname: fibbd
37 ;; #+begin_src emacs-lisp :var n=2 :results silent
38 ;; (fibbd n)
39 ;; #+end_src
40 ;;
41 ;; | original | fibbd |
42 ;; |----------+--------|
43 ;; | 0 | |
44 ;; | 1 | |
45 ;; | 2 | |
46 ;; | 3 | |
47 ;; | 4 | |
48 ;; | 5 | |
49 ;; | 6 | |
50 ;; | 7 | |
51 ;; | 8 | |
52 ;; | 9 | |
53 ;; #+TBLFM: $2='(sbe 'fibbd (n $1))
55 ;;; Code:
56 (require 'org-babel)
58 (defun org-babel-table-truncate-at-newline (string)
59 (if (and (stringp string) (string-match "[\n\r]" string))
60 (concat (substring string 0 (match-beginning 0)) "...")
61 string))
63 (defmacro sbe (source-block &rest variables)
64 "Return the results of calling SOURCE-BLOCK with all assigning
65 every variable in VARIABLES. Each element of VARIABLES should be
66 a two element list, whose first element is the name of the
67 variable and second element is a string of its value. The
68 following call to `sbe' would be equivalent to the following
69 source code block.
71 (sbe 'source-block (n 2) (m 3))
73 #+begin_src emacs-lisp :var results=source-block(n=2, m=3) :results silent
74 results
75 #+end_src"
76 (unless (stringp source-block) (setq source-block (symbol-name source-block)))
77 (org-babel-table-truncate-at-newline ;; org-table cells can't be multi-line
78 (if (and source-block (> (length source-block) 0))
79 (let ((params
80 (eval `(org-babel-parse-header-arguments
81 (concat ":var results="
82 ,source-block
83 "("
84 (mapconcat (lambda (var-spec)
85 (format "%S=%s" (first var-spec) (second var-spec)))
86 ',variables ", ")
87 ")")))))
88 (org-babel-execute-src-block t (list "emacs-lisp" "results" params)))
89 "")))
91 (provide 'org-babel-table)
92 ;;; org-babel-table.el ends here