ob-table.el 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.3
  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. (org-babel-table-truncate-at-newline ;; org-table cells can't be multi-line
  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. (provide 'ob-table)
  104. ;; arch-tag: 4234cc7c-4fc8-4e92-abb0-2892de1a493b
  105. ;;; ob-table.el ends here