ob-shell: honor the specified shell for :session
[org-mode.git] / lisp / ob-table.el
blob6658313387a358f19aaebc1e62e082e1c6d572c4
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 ;;; Code:
53 (require 'ob-core)
55 (defun org-babel-table-truncate-at-newline (string)
56 "Replace newline character with ellipses.
57 If STRING ends in a newline character, then remove the newline
58 character and replace it with ellipses."
59 (if (and (stringp string) (string-match "[\n\r]\\(.\\)?" string))
60 (concat (substring string 0 (match-beginning 0))
61 (if (match-string 1 string) "...")) string))
63 (defmacro org-sbe (source-block &rest variables)
64 "Return the results of calling SOURCE-BLOCK with VARIABLES.
66 Each element of VARIABLES should be a list of two elements: the
67 first element is the name of the variable and second element is a
68 string of its value.
70 So this `org-sbe' construct
72 (org-sbe 'source-block (n $2) (m 3))
74 is the equivalent of the following source code block:
76 #+begin_src emacs-lisp :var results=source-block(n=val_at_col_2, m=3) :results silent
77 results
78 #+end_src
80 NOTE: By default, string variable names are interpreted as
81 references to source-code blocks, to force interpretation of a
82 cell's value as a string, prefix the identifier a \"$\" (e.g.,
83 \"$$2\" instead of \"$2\" or \"$@2$2\" instead of \"@2$2\").
85 NOTE: It is also possible to pass header arguments to the code
86 block. In this case a table cell should hold the string value of
87 the header argument which can then be passed before all variables
88 as shown in the example below.
90 | 1 | 2 | :file nothing.png | nothing.png |
91 #+TBLFM: @1$4='(org-sbe test-sbe $3 (x $1) (y $2))"
92 (declare (debug (form form)))
93 (let* ((header-args (if (stringp (car variables)) (car variables) ""))
94 (variables (if (stringp (car variables)) (cdr variables) variables)))
95 (let* (quote
96 (variables
97 (mapcar
98 (lambda (var)
99 ;; ensure that all cells prefixed with $'s are strings
100 (cons (car var)
101 (delq nil (mapcar
102 (lambda (el)
103 (if (eq '$ el)
104 (prog1 nil (setq quote t))
105 (prog1
106 (cond
107 (quote (format "\"%s\"" el))
108 ((stringp el) (org-no-properties el))
109 (t el))
110 (setq quote nil))))
111 (cdr var)))))
112 variables)))
113 (unless (stringp source-block)
114 (setq source-block (symbol-name source-block)))
115 (let ((result
116 (if (and source-block (> (length source-block) 0))
117 (let ((params
118 ;; FIXME: Why `eval'?!?!?
119 (eval `(org-babel-parse-header-arguments
120 (concat
121 ":var results="
122 ,source-block
123 "[" ,header-args "]"
125 (mapconcat
126 (lambda (var-spec)
127 (if (> (length (cdr var-spec)) 1)
128 (format "%S='%S"
129 (car var-spec)
130 (mapcar #'read (cdr var-spec)))
131 (format "%S=%s"
132 (car var-spec) (cadr var-spec))))
133 ',variables ", ")
134 ")")))))
135 (org-babel-execute-src-block
136 nil (list "emacs-lisp" "results" params)
137 '((:results . "silent"))))
138 "")))
139 (org-babel-trim (if (stringp result) result (format "%S" result)))))))
141 (provide 'ob-table)
145 ;;; ob-table.el ends here