ob-groovy.el 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. ;;; ob-groovy.el --- Babel Functions for Groovy -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2013-2018 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 <https://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. (defvar org-babel-tangle-lang-exts) ;; Autoloaded
  26. (add-to-list 'org-babel-tangle-lang-exts '("groovy" . "groovy"))
  27. (defvar org-babel-default-header-args:groovy '())
  28. (defcustom org-babel-groovy-command "groovy"
  29. "Name of the command to use for executing Groovy code.
  30. May be either a command in the path, like groovy
  31. or an absolute path name, like /usr/local/bin/groovy
  32. parameters may be used, like groovy -v"
  33. :group 'org-babel
  34. :version "24.3"
  35. :type 'string)
  36. (defun org-babel-execute:groovy (body params)
  37. "Execute a block of Groovy code with org-babel. This function is
  38. called by `org-babel-execute-src-block'"
  39. (message "executing Groovy source code block")
  40. (let* ((processed-params (org-babel-process-params params))
  41. (session (org-babel-groovy-initiate-session (nth 0 processed-params)))
  42. (result-params (nth 2 processed-params))
  43. (result-type (cdr (assq :result-type params)))
  44. (full-body (org-babel-expand-body:generic
  45. body params))
  46. (result (org-babel-groovy-evaluate
  47. session full-body result-type result-params)))
  48. (org-babel-reassemble-table
  49. result
  50. (org-babel-pick-name
  51. (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
  52. (org-babel-pick-name
  53. (cdr (assq :rowname-names params)) (cdr (assq :rownames params))))))
  54. (defvar org-babel-groovy-wrapper-method
  55. "class Runner extends Script {
  56. def out = new PrintWriter(new ByteArrayOutputStream())
  57. def run() { %s }
  58. }
  59. println(new Runner().run())
  60. ")
  61. (defun org-babel-groovy-evaluate
  62. (session body &optional result-type result-params)
  63. "Evaluate BODY in external Groovy process.
  64. If RESULT-TYPE equals `output' then return standard output as a string.
  65. If RESULT-TYPE equals `value' then return the value of the last statement
  66. in BODY as elisp."
  67. (when session (error "Sessions are not (yet) supported for Groovy"))
  68. (pcase result-type
  69. (`output
  70. (let ((src-file (org-babel-temp-file "groovy-")))
  71. (progn (with-temp-file src-file (insert body))
  72. (org-babel-eval
  73. (concat org-babel-groovy-command " " src-file) ""))))
  74. (`value
  75. (let* ((src-file (org-babel-temp-file "groovy-"))
  76. (wrapper (format org-babel-groovy-wrapper-method body)))
  77. (with-temp-file src-file (insert wrapper))
  78. (let ((raw (org-babel-eval
  79. (concat org-babel-groovy-command " " src-file) "")))
  80. (org-babel-result-cond result-params
  81. raw
  82. (org-babel-script-escape raw)))))))
  83. (defun org-babel-prep-session:groovy (_session _params)
  84. "Prepare SESSION according to the header arguments specified in PARAMS."
  85. (error "Sessions are not (yet) supported for Groovy"))
  86. (defun org-babel-groovy-initiate-session (&optional _session)
  87. "If there is not a current inferior-process-buffer in SESSION
  88. then create. Return the initialized session. Sessions are not
  89. supported in Groovy."
  90. nil)
  91. (provide 'ob-groovy)
  92. ;;; ob-groovy.el ends here