customizable processing of Org-mode text used as comments in tangled source-code...
[org-mode.git] / lisp / ob-table.el
blobe253c552e0473479a7ba68e87615b1f3a4a03ec3
1 ;;; ob-table.el --- support for calling org-babel functions from tables
3 ;; Copyright (C) 2009-2011 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 `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 ;; #+srcname: 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='(sbe 'fibbd (n $1))
52 ;;; Code:
53 (require 'ob)
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 sbe (source-block &rest variables)
64 "Return the results of calling SOURCE-BLOCK with VARIABLES.
65 Each element of VARIABLES should be a two
66 element list, whose first element is the name of the variable and
67 second element is a string of its value. The following call to
68 `sbe' would be equivalent to the following source code block.
70 (sbe 'source-block (n $2) (m 3))
72 #+begin_src emacs-lisp :var results=source-block(n=val_at_col_2, m=3) :results silent
73 results
74 #+end_src
76 NOTE: by default string variable names are interpreted as
77 references to source-code blocks, to force interpretation of a
78 cell's value as a string, prefix the identifier with two \"$\"s
79 rather than a single \"$\" (i.e. \"$$2\" instead of \"$2\" in the
80 example above."
81 (let* (quote
82 (variables
83 (mapcar
84 (lambda (var)
85 ;; ensure that all cells prefixed with $'s are strings
86 (cons (car var)
87 (delq nil (mapcar
88 (lambda (el)
89 (if (eq '$ el)
90 (setq quote t)
91 (prog1 (if quote
92 (format "\"%s\"" el)
93 (org-babel-clean-text-properties el))
94 (setq quote nil))))
95 (cdr var)))))
96 variables)))
97 (unless (stringp source-block)
98 (setq source-block (symbol-name source-block)))
99 ((lambda (result)
100 (org-babel-trim (if (stringp result) result (format "%S" result))))
101 (if (and source-block (> (length source-block) 0))
102 (let ((params
103 (eval `(org-babel-parse-header-arguments
104 (concat ":var results="
105 ,source-block
107 (mapconcat
108 (lambda (var-spec)
109 (if (> (length (cdr var-spec)) 1)
110 (format "%S='%S"
111 (car var-spec)
112 (mapcar #'read (cdr var-spec)))
113 (format "%S=%s"
114 (car var-spec) (cadr var-spec))))
115 ',variables ", ")
116 ")")))))
117 (org-babel-execute-src-block
118 nil (list "emacs-lisp" "results" params) '((:results . "silent"))))
119 ""))))
120 (def-edebug-spec sbe (form form))
122 (provide 'ob-table)
126 ;;; ob-table.el ends here