Convert consecutive FSF copyright years to ranges.
[bpt/emacs.git] / lisp / org / ob-table.el
CommitLineData
86fbb8ca
CD
1;;; ob-table.el --- support for calling org-babel functions from tables
2
73b0cd50 3;; Copyright (C) 2009-2011 Free Software Foundation, Inc.
86fbb8ca
CD
4
5;; Author: Eric Schulte
6;; Keywords: literate programming, reproducible research
7;; Homepage: http://orgmode.org
acedf35c 8;; Version: 7.4
86fbb8ca
CD
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs 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 of the License, or
15;; (at your option) any later version.
16
17;; GNU Emacs 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.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25;;; Commentary:
26
27;; Should allow calling functions from org-mode tables using the
28;; function `sbe' as so...
29
30;; #+begin_src emacs-lisp :results silent
31;; (defun fibbd (n) (if (< n 2) 1 (+ (fibbd (- n 1)) (fibbd (- n 2)))))
32;; #+end_src
33
34;; #+srcname: fibbd
35;; #+begin_src emacs-lisp :var n=2 :results silent
36;; (fibbd n)
37;; #+end_src
38
39;; | original | fibbd |
40;; |----------+--------|
41;; | 0 | |
42;; | 1 | |
43;; | 2 | |
44;; | 3 | |
45;; | 4 | |
46;; | 5 | |
47;; | 6 | |
48;; | 7 | |
49;; | 8 | |
50;; | 9 | |
51;; #+TBLFM: $2='(sbe 'fibbd (n $1))
52
53;;; Code:
54(require 'ob)
55
56(defun org-babel-table-truncate-at-newline (string)
57 "Replace newline character with ellipses.
58If STRING ends in a newline character, then remove the newline
59character and replace it with ellipses."
acedf35c
CD
60 (if (and (stringp string) (string-match "[\n\r]\\(.\\)?" string))
61 (concat (substring string 0 (match-beginning 0))
62 (if (match-string 1 string) "...")) string))
86fbb8ca
CD
63
64(defmacro sbe (source-block &rest variables)
65 "Return the results of calling SOURCE-BLOCK with VARIABLES.
66Each element of VARIABLES should be a two
67element list, whose first element is the name of the variable and
68second element is a string of its value. The following call to
69`sbe' would be equivalent to the following source code block.
70
71 (sbe 'source-block (n $2) (m 3))
72
73#+begin_src emacs-lisp :var results=source-block(n=val_at_col_2, m=3) :results silent
74results
75#+end_src
76
77NOTE: by default string variable names are interpreted as
78references to source-code blocks, to force interpretation of a
79cell's value as a string, prefix the identifier with two \"$\"s
80rather than a single \"$\" (i.e. \"$$2\" instead of \"$2\" in the
81example above."
afe98dfa
CD
82 (let* (quote
83 (variables
84 (mapcar
85 (lambda (var)
86 ;; ensure that all cells prefixed with $'s are strings
87 (cons (car var)
88 (delq nil (mapcar
89 (lambda (el)
90 (if (eq '$ el)
91 (setq quote t)
92 (prog1 (if quote
93 (format "\"%s\"" el)
94 (org-babel-clean-text-properties el))
95 (setq quote nil))))
96 (cdr var)))))
97 variables)))
98 (unless (stringp source-block)
99 (setq source-block (symbol-name source-block)))
86fbb8ca
CD
100 (org-babel-table-truncate-at-newline ;; org-table cells can't be multi-line
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
106 "("
afe98dfa
CD
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 ", ")
86fbb8ca
CD
116 ")")))))
117 (org-babel-execute-src-block
afe98dfa 118 nil (list "emacs-lisp" "results" params) '((:results . "silent"))))
86fbb8ca
CD
119 ""))))
120
121(provide 'ob-table)
122
86fbb8ca
CD
123
124;;; ob-table.el ends here