ob-groovy.el 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. ;;; ob-groovy.el --- Babel Functions for Groovy -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2013-2022 Free Software Foundation, Inc.
  3. ;; Author: Miro Bezjak <bezjak.miro@gmail.com>
  4. ;; Maintainer: Palak Mathur <palakmathur@gmail.com>
  5. ;; Keywords: literate programming, reproducible research
  6. ;; URL: https://orgmode.org
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Currently only supports the external execution. No session support yet.
  20. ;;; Requirements:
  21. ;; - Groovy language :: https://groovy-lang.org
  22. ;; - Groovy major mode :: Can be installed from MELPA or
  23. ;; https://github.com/russel/Emacs-Groovy-Mode
  24. ;;; Code:
  25. (require 'ob)
  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.
  39. This function is 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 (assq :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 (assq :colname-names params)) (cdr (assq :colnames params)))
  53. (org-babel-pick-name
  54. (cdr (assq :rowname-names params)) (cdr (assq :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. (pcase 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