ob-groovy.el 4.2 KB

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