ob-table.el 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. ;;; ob-table.el --- support for calling org-babel functions from tables
  2. ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 7.5
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Should allow calling functions from org-mode tables using the
  20. ;; function `sbe' as so...
  21. ;; #+begin_src emacs-lisp :results silent
  22. ;; (defun fibbd (n) (if (< n 2) 1 (+ (fibbd (- n 1)) (fibbd (- n 2)))))
  23. ;; #+end_src
  24. ;; #+srcname: fibbd
  25. ;; #+begin_src emacs-lisp :var n=2 :results silent
  26. ;; (fibbd n)
  27. ;; #+end_src
  28. ;; | original | fibbd |
  29. ;; |----------+--------|
  30. ;; | 0 | |
  31. ;; | 1 | |
  32. ;; | 2 | |
  33. ;; | 3 | |
  34. ;; | 4 | |
  35. ;; | 5 | |
  36. ;; | 6 | |
  37. ;; | 7 | |
  38. ;; | 8 | |
  39. ;; | 9 | |
  40. ;; #+TBLFM: $2='(sbe 'fibbd (n $1))
  41. ;;; Code:
  42. (require 'ob)
  43. (defun org-babel-table-truncate-at-newline (string)
  44. "Replace newline character with ellipses.
  45. If STRING ends in a newline character, then remove the newline
  46. character and replace it with ellipses."
  47. (if (and (stringp string) (string-match "[\n\r]\\(.\\)?" string))
  48. (concat (substring string 0 (match-beginning 0))
  49. (if (match-string 1 string) "...")) string))
  50. (defmacro sbe (source-block &rest variables)
  51. "Return the results of calling SOURCE-BLOCK with VARIABLES.
  52. Each element of VARIABLES should be a two
  53. element list, whose first element is the name of the variable and
  54. second element is a string of its value. The following call to
  55. `sbe' would be equivalent to the following source code block.
  56. (sbe 'source-block (n $2) (m 3))
  57. #+begin_src emacs-lisp :var results=source-block(n=val_at_col_2, m=3) :results silent
  58. results
  59. #+end_src
  60. NOTE: by default string variable names are interpreted as
  61. references to source-code blocks, to force interpretation of a
  62. cell's value as a string, prefix the identifier with two \"$\"s
  63. rather than a single \"$\" (i.e. \"$$2\" instead of \"$2\" in the
  64. example above."
  65. (let* (quote
  66. (variables
  67. (mapcar
  68. (lambda (var)
  69. ;; ensure that all cells prefixed with $'s are strings
  70. (cons (car var)
  71. (delq nil (mapcar
  72. (lambda (el)
  73. (if (eq '$ el)
  74. (setq quote t)
  75. (prog1 (if quote
  76. (format "\"%s\"" el)
  77. (org-babel-clean-text-properties el))
  78. (setq quote nil))))
  79. (cdr var)))))
  80. variables)))
  81. (unless (stringp source-block)
  82. (setq source-block (symbol-name source-block)))
  83. ((lambda (result)
  84. (org-babel-trim (if (stringp result) result (format "%S" result))))
  85. (if (and source-block (> (length source-block) 0))
  86. (let ((params
  87. (eval `(org-babel-parse-header-arguments
  88. (concat ":var results="
  89. ,source-block
  90. "("
  91. (mapconcat
  92. (lambda (var-spec)
  93. (if (> (length (cdr var-spec)) 1)
  94. (format "%S='%S"
  95. (car var-spec)
  96. (mapcar #'read (cdr var-spec)))
  97. (format "%S=%s"
  98. (car var-spec) (cadr var-spec))))
  99. ',variables ", ")
  100. ")")))))
  101. (org-babel-execute-src-block
  102. nil (list "emacs-lisp" "results" params) '((:results . "silent"))))
  103. ""))))
  104. (provide 'ob-table)
  105. ;; arch-tag: 4234cc7c-4fc8-4e92-abb0-2892de1a493b
  106. ;;; ob-table.el ends here