ob-shen.el 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. ;;; ob-shen.el --- org-babel functions for Shen
  2. ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
  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. (result-params (cdr (assoc :result-params params)))
  51. (full-body (org-babel-expand-body:shen body params)))
  52. ((lambda (results)
  53. (if (or (member 'scalar result-params)
  54. (member 'verbatim result-params))
  55. results
  56. (condition-case nil (org-babel-script-escape results)
  57. (error results))))
  58. (with-temp-buffer
  59. (insert full-body)
  60. (call-interactively #'shen-eval-defun)))))
  61. (provide 'ob-shen)
  62. ;;; ob-shen.el ends here