ob-groovy.el 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. ;;; ob-groovy.el --- org-babel functions for Groovy evaluation
  2. ;; Copyright (C) 2013-2016 Free Software Foundation, Inc.
  3. ;; Author: Miro Bezjak
  4. ;; Keywords: literate programming, reproducible research
  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 only supports the external execution. No session support yet.
  19. ;;; Requirements:
  20. ;; - Groovy language :: http://groovy.codehaus.org
  21. ;; - Groovy major mode :: Can be installed from MELPA or
  22. ;; https://github.com/russel/Emacs-Groovy-Mode
  23. ;;; Code:
  24. (require 'ob)
  25. (eval-when-compile (require 'cl))
  26. (defvar org-babel-tangle-lang-exts) ;; Autoloaded
  27. (add-to-list 'org-babel-tangle-lang-exts '("groovy" . "groovy"))
  28. (defvar org-babel-default-header-args:groovy '())
  29. (defcustom org-babel-groovy-command "groovy"
  30. "Name of the command to use for executing Groovy code.
  31. May be either a command in the path, like groovy
  32. or an absolute path name, like /usr/local/bin/groovy
  33. parameters may be used, like groovy -v"
  34. :group 'org-babel
  35. :version "24.3"
  36. :type 'string)
  37. (defun org-babel-execute:groovy (body params)
  38. "Execute a block of Groovy code with org-babel. This function is
  39. called by `org-babel-execute-src-block'"
  40. (message "executing Groovy source code block")
  41. (let* ((processed-params (org-babel-process-params params))
  42. (session (org-babel-groovy-initiate-session (nth 0 processed-params)))
  43. (vars (nth 1 processed-params))
  44. (result-params (nth 2 processed-params))
  45. (result-type (cdr (assoc :result-type params)))
  46. (full-body (org-babel-expand-body:generic
  47. body params))
  48. (result (org-babel-groovy-evaluate
  49. session full-body result-type result-params)))
  50. (org-babel-reassemble-table
  51. result
  52. (org-babel-pick-name
  53. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  54. (org-babel-pick-name
  55. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
  56. (defvar org-babel-groovy-wrapper-method
  57. "class Runner extends Script {
  58. def out = new PrintWriter(new ByteArrayOutputStream())
  59. def run() { %s }
  60. }
  61. println(new Runner().run())
  62. ")
  63. (defun org-babel-groovy-evaluate
  64. (session body &optional result-type result-params)
  65. "Evaluate BODY in external Groovy process.
  66. If RESULT-TYPE equals `output' then return standard output as a string.
  67. If RESULT-TYPE equals `value' then return the value of the last statement
  68. in BODY as elisp."
  69. (when session (error "Sessions are not (yet) supported for Groovy"))
  70. (case result-type
  71. (output
  72. (let ((src-file (org-babel-temp-file "groovy-")))
  73. (progn (with-temp-file src-file (insert body))
  74. (org-babel-eval
  75. (concat org-babel-groovy-command " " src-file) ""))))
  76. (value
  77. (let* ((src-file (org-babel-temp-file "groovy-"))
  78. (wrapper (format org-babel-groovy-wrapper-method body)))
  79. (with-temp-file src-file (insert wrapper))
  80. (let ((raw (org-babel-eval
  81. (concat org-babel-groovy-command " " src-file) "")))
  82. (org-babel-result-cond result-params
  83. raw
  84. (org-babel-script-escape raw)))))))
  85. (defun org-babel-prep-session:groovy (session params)
  86. "Prepare SESSION according to the header arguments specified in PARAMS."
  87. (error "Sessions are not (yet) supported for Groovy"))
  88. (defun org-babel-groovy-initiate-session (&optional session)
  89. "If there is not a current inferior-process-buffer in SESSION
  90. then create. Return the initialized session. Sessions are not
  91. supported in Groovy."
  92. nil)
  93. (provide 'ob-groovy)
  94. ;;; ob-groovy.el ends here