ob-shen.el 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. ;;; ob-shen.el --- Babel Functions for Shen -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2018 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 <https://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. (declare-function org-babel-ruby-var-to-ruby "ob-ruby" (var))
  28. (defvar org-babel-default-header-args:shen '()
  29. "Default header arguments for shen code blocks.")
  30. (defun org-babel-expand-body:shen (body params)
  31. "Expand BODY according to PARAMS, return the expanded body."
  32. (let ((vars (org-babel--get-vars params)))
  33. (if (> (length vars) 0)
  34. (concat "(let "
  35. (mapconcat (lambda (var)
  36. (format "%s %s" (car var)
  37. (org-babel-shen-var-to-shen (cdr var))))
  38. vars " ")
  39. body ")")
  40. body)))
  41. (defun org-babel-shen-var-to-shen (var)
  42. "Convert VAR into a shen variable."
  43. (if (listp var)
  44. (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var " ") "]")
  45. (format "%S" var)))
  46. (defun org-babel-execute:shen (body params)
  47. "Execute a block of Shen code with org-babel.
  48. This function is called by `org-babel-execute-src-block'"
  49. (require 'inf-shen)
  50. (let* ((result-params (cdr (assq :result-params params)))
  51. (full-body (org-babel-expand-body:shen body params)))
  52. (let ((results
  53. (with-temp-buffer
  54. (insert full-body)
  55. (call-interactively #'shen-eval-defun))))
  56. (org-babel-result-cond result-params
  57. results
  58. (condition-case nil (org-babel-script-escape results)
  59. (error results))))))
  60. (provide 'ob-shen)
  61. ;;; ob-shen.el ends here