org-babel-table.el 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. ;;; org-babel-table.el --- integration for calling org-babel functions from tables
  2. ;; Copyright (C) 2009 Eric Schulte
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program 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, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; Should allow calling functions from org-mode tables using the
  24. ;; function `sbe' as so...
  25. ;;
  26. ;; #+begin_src emacs-lisp :results silent
  27. ;; (defun fibbd (n) (if (< n 2) 1 (+ (fibbd (- n 1)) (fibbd (- n 2)))))
  28. ;; #+end_src
  29. ;;
  30. ;; #+srcname: fibbd
  31. ;; #+begin_src emacs-lisp :var n=2 :results silent
  32. ;; (fibbd n)
  33. ;; #+end_src
  34. ;;
  35. ;; | original | fibbd |
  36. ;; |----------+--------|
  37. ;; | 0 | |
  38. ;; | 1 | |
  39. ;; | 2 | |
  40. ;; | 3 | |
  41. ;; | 4 | |
  42. ;; | 5 | |
  43. ;; | 6 | |
  44. ;; | 7 | |
  45. ;; | 8 | |
  46. ;; | 9 | |
  47. ;; #+TBLFM: $2='(sbe 'fibbd (n $1))
  48. ;;; Code:
  49. (require 'org-babel)
  50. (defun org-babel-table-truncate-at-newline (string)
  51. (if (and (stringp string) (string-match "[\n\r]" string))
  52. (concat (substring string 0 (match-beginning 0)) "...")
  53. string))
  54. (defmacro sbe (source-block &rest variables)
  55. "Return the results of calling SOURCE-BLOCK assigning every
  56. variable in VARIABLES. Each element of VARIABLES should be a two
  57. element list, whose first element is the name of the variable and
  58. second element is a string of its value. The following call to
  59. `sbe' would be equivalent to the following source code block.
  60. (sbe 'source-block (n $2) (m 3))
  61. #+begin_src emacs-lisp :var results=source-block(n=val_at_col_2, m=3) :results silent
  62. results
  63. #+end_src
  64. NOTE: by default string variable names are interpreted as
  65. references to source-code blocks, to force interpretation of a
  66. cell's value as a string, prefix the identifier with two \"$\"s
  67. rather than a single \"$\" (i.e. \"$$2\" instead of \"$2\" in the
  68. example above."
  69. (let ((variables (mapcar
  70. (lambda (var)
  71. (if (and (= 3 (length var)) (eq (second var) '$))
  72. (list (car var) (format "\"%s\"" (last var)))
  73. var))
  74. variables)))
  75. (unless (stringp source-block) (setq source-block (symbol-name source-block)))
  76. (org-babel-table-truncate-at-newline ;; org-table cells can't be multi-line
  77. (if (and source-block (> (length source-block) 0))
  78. (let ((params
  79. (eval `(org-babel-parse-header-arguments
  80. (concat ":var results="
  81. ,source-block
  82. "("
  83. (mapconcat (lambda (var-spec)
  84. (format "%S=%s" (first var-spec) (second var-spec)))
  85. ',variables ", ")
  86. ")")))))
  87. (org-babel-execute-src-block
  88. nil (list "emacs-lisp" "results"
  89. (org-babel-merge-params '((:results . "silent")) params))))
  90. ""))))
  91. (provide 'org-babel-table)
  92. ;;; org-babel-table.el ends here