ob-table.el 5.2 KB

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