litorgy-table.el 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. (let ((params (eval `(litorgy-parse-header-arguments
  62. (concat ":var results="
  63. (symbol-name ,source-block)
  64. "("
  65. (mapconcat (lambda (var-spec)
  66. (format "%S=%s" (first var-spec) (second var-spec)))
  67. ',variables ", ")
  68. ")")))))
  69. (litorgy-execute-src-block
  70. nil (list "emacs-lisp" "results" (org-combine-plists params '((:results . "silent")))))))
  71. (provide 'litorgy-table)
  72. ;;; litorgy-table.el ends here