Merge branch 'maint'
[org-mode.git] / lisp / ob-table.el
blobb2a8da64dffe0f98a3949fb9532eee2041b23498
1 ;;; ob-table.el --- support for calling org-babel functions from tables
3 ;; Copyright (C) 2009-2014 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Should allow calling functions from org-mode tables using the
27 ;; function `org-sbe' as so...
29 ;; #+begin_src emacs-lisp :results silent
30 ;; (defun fibbd (n) (if (< n 2) 1 (+ (fibbd (- n 1)) (fibbd (- n 2)))))
31 ;; #+end_src
33 ;; #+name: fibbd
34 ;; #+begin_src emacs-lisp :var n=2 :results silent
35 ;; (fibbd n)
36 ;; #+end_src
38 ;; | original | fibbd |
39 ;; |----------+--------|
40 ;; | 0 | |
41 ;; | 1 | |
42 ;; | 2 | |
43 ;; | 3 | |
44 ;; | 4 | |
45 ;; | 5 | |
46 ;; | 6 | |
47 ;; | 7 | |
48 ;; | 8 | |
49 ;; | 9 | |
50 ;; #+TBLFM: $2='(org-sbe "fibbd" (n $1))
52 ;; NOTE: The quotation marks around the function name, 'fibbd' here,
53 ;; are optional.
55 ;;; Code:
56 (require 'ob-core)
58 (defun org-babel-table-truncate-at-newline (string)
59 "Replace newline character with ellipses.
60 If STRING ends in a newline character, then remove the newline
61 character and replace it with ellipses."
62 (if (and (stringp string) (string-match "[\n\r]\\(.\\)?" string))
63 (concat (substring string 0 (match-beginning 0))
64 (if (match-string 1 string) "...")) string))
66 (defmacro org-sbe (source-block &rest variables)
67 "Return the results of calling SOURCE-BLOCK with VARIABLES.
69 Each element of VARIABLES should be a list of two elements: the
70 first element is the name of the variable and second element is a
71 string of its value.
73 So this `org-sbe' construct
75 (org-sbe \"source-block\" (n $2) (m 3))
77 is the equivalent of the following source code block:
79 #+begin_src emacs-lisp :var results=source-block(n=val_at_col_2, m=3) :results silent
80 results
81 #+end_src
83 NOTE: The quotation marks around the function name,
84 'source-block', are optional.
86 NOTE: By default, string variable names are interpreted as
87 references to source-code blocks, to force interpretation of a
88 cell's value as a string, prefix the identifier a \"$\" (e.g.,
89 \"$$2\" instead of \"$2\" or \"$@2$2\" instead of \"@2$2\").
91 NOTE: It is also possible to pass header arguments to the code
92 block. In this case a table cell should hold the string value of
93 the header argument which can then be passed before all variables
94 as shown in the example below.
96 | 1 | 2 | :file nothing.png | nothing.png |
97 #+TBLFM: @1$4='(org-sbe test-sbe $3 (x $1) (y $2))"
98 (declare (debug (form form)))
99 (let* ((header-args (if (stringp (car variables)) (car variables) ""))
100 (variables (if (stringp (car variables)) (cdr variables) variables)))
101 (let* (quote
102 (variables
103 (mapcar
104 (lambda (var)
105 ;; ensure that all cells prefixed with $'s are strings
106 (cons (car var)
107 (delq nil (mapcar
108 (lambda (el)
109 (if (eq '$ el)
110 (prog1 nil (setq quote t))
111 (prog1
112 (cond
113 (quote (format "\"%s\"" el))
114 ((stringp el) (org-no-properties el))
115 (t el))
116 (setq quote nil))))
117 (cdr var)))))
118 variables)))
119 (unless (stringp source-block)
120 (setq source-block (symbol-name source-block)))
121 (let ((result
122 (if (and source-block (> (length source-block) 0))
123 (let ((params
124 ;; FIXME: Why `eval'?!?!?
125 (eval `(org-babel-parse-header-arguments
126 (concat
127 ":var results="
128 ,source-block
129 "[" ,header-args "]"
131 (mapconcat
132 (lambda (var-spec)
133 (if (> (length (cdr var-spec)) 1)
134 (format "%S='%S"
135 (car var-spec)
136 (mapcar #'read (cdr var-spec)))
137 (format "%S=%s"
138 (car var-spec) (cadr var-spec))))
139 ',variables ", ")
140 ")")))))
141 (org-babel-execute-src-block
142 nil (list "emacs-lisp" "results" params)
143 '((:results . "silent"))))
144 "")))
145 (org-babel-trim (if (stringp result) result (format "%S" result)))))))
147 (provide 'ob-table)
151 ;;; ob-table.el ends here