ob-groovy.el 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. ;;; ob-groovy.el --- Babel Functions for Groovy -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2013-2015 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. (result-params (nth 2 processed-params))
  44. (result-type (cdr (assoc :result-type params)))
  45. (full-body (org-babel-expand-body:generic
  46. body params))
  47. (result (org-babel-groovy-evaluate
  48. session full-body result-type result-params)))
  49. (org-babel-reassemble-table
  50. result
  51. (org-babel-pick-name
  52. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  53. (org-babel-pick-name
  54. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
  55. (defvar org-babel-groovy-wrapper-method
  56. "class Runner extends Script {
  57. def out = new PrintWriter(new ByteArrayOutputStream())
  58. def run() { %s }
  59. }
  60. println(new Runner().run())
  61. ")
  62. (defun org-babel-groovy-evaluate
  63. (session body &optional result-type result-params)
  64. "Evaluate BODY in external Groovy process.
  65. If RESULT-TYPE equals `output' then return standard output as a string.
  66. If RESULT-TYPE equals `value' then return the value of the last statement
  67. in BODY as elisp."
  68. (when session (error "Sessions are not (yet) supported for Groovy"))
  69. (case result-type
  70. (output
  71. (let ((src-file (org-babel-temp-file "groovy-")))
  72. (progn (with-temp-file src-file (insert body))
  73. (org-babel-eval
  74. (concat org-babel-groovy-command " " src-file) ""))))
  75. (value
  76. (let* ((src-file (org-babel-temp-file "groovy-"))
  77. (wrapper (format org-babel-groovy-wrapper-method body)))
  78. (with-temp-file src-file (insert wrapper))
  79. (let ((raw (org-babel-eval
  80. (concat org-babel-groovy-command " " src-file) "")))
  81. (org-babel-result-cond result-params
  82. raw
  83. (org-babel-script-escape raw)))))))
  84. (defun org-babel-prep-session:groovy (_session _params)
  85. "Prepare SESSION according to the header arguments specified in PARAMS."
  86. (error "Sessions are not (yet) supported for Groovy"))
  87. (defun org-babel-groovy-initiate-session (&optional _session)
  88. "If there is not a current inferior-process-buffer in SESSION
  89. then create. Return the initialized session. Sessions are not
  90. supported in Groovy."
  91. nil)
  92. (provide 'ob-groovy)
  93. ;;; ob-groovy.el ends here