ob-shen.el 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ;;; ob-shen.el --- org-babel functions for Shen
  2. ;; Copyright (C) 2010-2011 Free Software Foundation
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research, shen
  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. ;; Currently this only works using session evaluation as there is no
  19. ;; defined method for executing shen code outside of a session.
  20. ;;; Requirements:
  21. ;; - shen-mode and inf-shen will soon be available through the GNU
  22. ;; elpa, however in the interim they are available at
  23. ;; https://github.com/eschulte/shen-mode
  24. ;;; Code:
  25. (require 'ob)
  26. (declare-function shen-eval-defun "ext:inf-shen" (&optional and-go))
  27. (defvar org-babel-default-header-args:shen '()
  28. "Default header arguments for shen code blocks.")
  29. (defun org-babel-expand-body:shen (body params)
  30. "Expand BODY according to PARAMS, return the expanded body."
  31. (let ((vars (mapcar #'cdr (org-babel-get-header params :var))))
  32. (if (> (length vars) 0)
  33. (concat "(let "
  34. (mapconcat (lambda (var)
  35. (format "%s %s" (car var)
  36. (org-babel-shen-var-to-shen (cdr var))))
  37. vars " ")
  38. body ")")
  39. body)))
  40. (defun org-babel-shen-var-to-shen (var)
  41. "Convert VAR into a shen variable."
  42. (if (listp var)
  43. (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var " ") "]")
  44. (format "%S" var)))
  45. (defun org-babel-execute:shen (body params)
  46. "Execute a block of Shen code with org-babel.
  47. This function is called by `org-babel-execute-src-block'"
  48. (require 'inf-shen)
  49. (let* ((result-type (cdr (assoc :result-type params)))
  50. (full-body (org-babel-expand-body:shen body params)))
  51. ((lambda (results)
  52. (if (or (member 'scalar result-params)
  53. (member 'verbatim result-params))
  54. results
  55. (condition-case nil (org-babel-script-escape results)
  56. (error result))))
  57. (with-temp-buffer
  58. (insert full-body)
  59. (call-interactively #'shen-eval-defun)))))
  60. (provide 'ob-shen)
  61. ;;; ob-shen.el ends here