Remove `org-context-p'
[org-mode/org-tableheadings.git] / lisp / ob-table.el
blobd11df9abcfa02d104c0e47f5fe8a6e12b72cc4b5
1 ;;; ob-table.el --- Support for Calling Babel Functions from Tables -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2018 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: https://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 <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Should allow calling functions from Org tables using the function
27 ;; `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 (declare-function org-trim "org" (s &optional keep-lead))
60 (defun org-babel-table-truncate-at-newline (string)
61 "Replace newline character with ellipses.
62 If STRING ends in a newline character, then remove the newline
63 character and replace it with ellipses."
64 (if (and (stringp string) (string-match "[\n\r]\\(.\\)?" string))
65 (concat (substring string 0 (match-beginning 0))
66 (when (match-string 1 string) "...")) string))
68 (defmacro org-sbe (source-block &rest variables)
69 "Return the results of calling SOURCE-BLOCK with VARIABLES.
71 Each element of VARIABLES should be a list of two elements: the
72 first element is the name of the variable and second element is a
73 string of its value.
75 So this `org-sbe' construct
77 (org-sbe \"source-block\" (n $2) (m 3))
79 is the equivalent of the following source code block:
81 #+begin_src emacs-lisp :var results=source-block(n=val_at_col_2, m=3) :results silent
82 results
83 #+end_src
85 NOTE: The quotation marks around the function name,
86 'source-block', are optional.
88 NOTE: By default, string variable names are interpreted as
89 references to source-code blocks, to force interpretation of a
90 cell's value as a string, prefix the identifier a \"$\" (e.g.,
91 \"$$2\" instead of \"$2\" or \"$@2$2\" instead of \"@2$2\").
92 This will not work with a range; instead, pass it as a list,
93 e.g. (org-sbe fun (r (list $1..$2))).
95 NOTE: It is also possible to pass header arguments to the code
96 block. In this case a table cell should hold the string value of
97 the header argument which can then be passed before all variables
98 as shown in the example below.
100 | 1 | 2 | :file nothing.png | nothing.png |
101 #+TBLFM: @1$4=\\='(org-sbe test-sbe $3 (x $1) (y $2))"
102 (declare (debug (form form)))
103 (let* ((header-args (if (stringp (car variables)) (car variables) ""))
104 (variables (if (stringp (car variables)) (cdr variables) variables)))
105 (let* (quote
106 (variables
107 (mapcar
108 (lambda (var)
109 ;; ensure that all cells prefixed with $'s are strings
110 (cons (car var)
111 (delq nil (mapcar
112 (lambda (el)
113 (if (eq '$ el)
114 (prog1 nil (setq quote t))
115 (prog1
116 (cond
117 (quote (format "%S" el))
118 ((stringp el) (org-no-properties el))
119 (t el))
120 (setq quote nil))))
121 (cdr var)))))
122 variables)))
123 (unless (stringp source-block)
124 (setq source-block (symbol-name source-block)))
125 (let ((result
126 (if (and source-block (> (length source-block) 0))
127 (let ((params
128 ;; FIXME: Why `eval'?!?!?
129 (eval `(org-babel-parse-header-arguments
130 (concat
131 ":var results="
132 ,source-block
133 "[" ,header-args "]"
135 (mapconcat
136 (lambda (var-spec)
137 (cond
138 ((> (length (cdr var-spec)) 1)
139 (format "%S='%S"
140 (car var-spec)
141 (mapcar #'read (cdr var-spec))))
142 ((stringp (cadr var-spec))
143 (format "%S=%s"
144 (car var-spec) (cadr var-spec)))
146 (format "%S=%S"
147 (car var-spec) (cadr var-spec)))))
148 ',variables ", ")
149 ")")))))
150 (org-babel-execute-src-block
151 nil (list "emacs-lisp" "results" params)
152 '((:results . "silent"))))
153 "")))
154 (org-trim (if (stringp result) result (format "%S" result)))))))
156 (provide 'ob-table)
160 ;;; ob-table.el ends here