litorgy-table.el 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. ;;; litorgy-table.el --- integration for calling litorgical 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 'litorgy)
  50. (defmacro sbe (source-block &rest variables)
  51. "Return the results of calling SOURCE-BLOCK with all assigning
  52. every variable in VARIABLES. Each element of VARIABLES should be
  53. a two element list, who's first element is the name of the
  54. variable and second element is a string of it's value. The
  55. following call to `sbe' would be equivalent to the following
  56. source code block.
  57. (sbe 'source-block (n 2) (m 3))
  58. #+begin_src emacs-lisp :var results=source-block(n=2, m=3) :results silent
  59. results
  60. #+end_src"
  61. (unless (stringp source-block) (setq source-block (symbol-name source-block)))
  62. (if (and source-block (> (length source-block) 0))
  63. (let ((params (eval `(litorgy-parse-header-arguments
  64. (concat ":var results="
  65. ,source-block
  66. "("
  67. (mapconcat (lambda (var-spec)
  68. (format "%S=%s" (first var-spec) (second var-spec)))
  69. ',variables ", ")
  70. ")")))))
  71. (litorgy-execute-src-block t (list "emacs-lisp" "results" params)))
  72. ""))
  73. (provide 'litorgy-table)
  74. ;;; litorgy-table.el ends here