ob-table.el 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. ;;; ob-table.el --- support for calling org-babel functions from tables
  2. ;; Copyright (C) 2009-2011 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://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 <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Should allow calling functions from org-mode tables using the
  19. ;; function `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. ;; #+srcname: 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='(sbe 'fibbd (n $1))
  40. ;;; Code:
  41. (require 'ob)
  42. (defun org-babel-table-truncate-at-newline (string)
  43. "Replace newline character with ellipses.
  44. If STRING ends in a newline character, then remove the newline
  45. character and replace it with ellipses."
  46. (if (and (stringp string) (string-match "[\n\r]\\(.\\)?" string))
  47. (concat (substring string 0 (match-beginning 0))
  48. (if (match-string 1 string) "...")) string))
  49. (defmacro sbe (source-block &rest variables)
  50. "Return the results of calling SOURCE-BLOCK with VARIABLES.
  51. Each element of VARIABLES should be a two
  52. element list, whose first element is the name of the variable and
  53. second element is a string of its value. The following call to
  54. `sbe' would be equivalent to the following source code block.
  55. (sbe 'source-block (n $2) (m 3))
  56. #+begin_src emacs-lisp :var results=source-block(n=val_at_col_2, m=3) :results silent
  57. results
  58. #+end_src
  59. NOTE: by default string variable names are interpreted as
  60. references to source-code blocks, to force interpretation of a
  61. cell's value as a string, prefix the identifier with two \"$\"s
  62. rather than a single \"$\" (i.e. \"$$2\" instead of \"$2\" in the
  63. example above."
  64. (let* (quote
  65. (variables
  66. (mapcar
  67. (lambda (var)
  68. ;; ensure that all cells prefixed with $'s are strings
  69. (cons (car var)
  70. (delq nil (mapcar
  71. (lambda (el)
  72. (if (eq '$ el)
  73. (setq quote t)
  74. (prog1 (if quote
  75. (format "\"%s\"" el)
  76. (org-babel-clean-text-properties el))
  77. (setq quote nil))))
  78. (cdr var)))))
  79. variables)))
  80. (unless (stringp source-block)
  81. (setq source-block (symbol-name source-block)))
  82. ((lambda (result)
  83. (org-babel-trim (if (stringp result) result (format "%S" result))))
  84. (if (and source-block (> (length source-block) 0))
  85. (let ((params
  86. (eval `(org-babel-parse-header-arguments
  87. (concat ":var results="
  88. ,source-block
  89. "("
  90. (mapconcat
  91. (lambda (var-spec)
  92. (if (> (length (cdr var-spec)) 1)
  93. (format "%S='%S"
  94. (car var-spec)
  95. (mapcar #'read (cdr var-spec)))
  96. (format "%S=%s"
  97. (car var-spec) (cadr var-spec))))
  98. ',variables ", ")
  99. ")")))))
  100. (org-babel-execute-src-block
  101. nil (list "emacs-lisp" "results" params) '((:results . "silent"))))
  102. ""))))
  103. (def-edebug-spec sbe (form form))
  104. (provide 'ob-table)
  105. ;;; ob-table.el ends here