Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / ob-groovy.el
blob9578c70dc48428fd182feb85a2231992a6b6662b
1 ;;; ob-groovy.el --- Babel Functions for Groovy -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2013-2016 Free Software Foundation, Inc.
5 ;; Author: Miro Bezjak
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
25 ;; Currently only supports the external execution. No session support yet.
27 ;;; Requirements:
28 ;; - Groovy language :: http://groovy.codehaus.org
29 ;; - Groovy major mode :: Can be installed from MELPA or
30 ;; https://github.com/russel/Emacs-Groovy-Mode
32 ;;; Code:
33 (require 'ob)
35 (defvar org-babel-tangle-lang-exts) ;; Autoloaded
36 (add-to-list 'org-babel-tangle-lang-exts '("groovy" . "groovy"))
37 (defvar org-babel-default-header-args:groovy '())
38 (defcustom org-babel-groovy-command "groovy"
39 "Name of the command to use for executing Groovy code.
40 May be either a command in the path, like groovy
41 or an absolute path name, like /usr/local/bin/groovy
42 parameters may be used, like groovy -v"
43 :group 'org-babel
44 :version "24.3"
45 :type 'string)
47 (defun org-babel-execute:groovy (body params)
48 "Execute a block of Groovy code with org-babel. This function is
49 called by `org-babel-execute-src-block'"
50 (message "executing Groovy source code block")
51 (let* ((processed-params (org-babel-process-params params))
52 (session (org-babel-groovy-initiate-session (nth 0 processed-params)))
53 (result-params (nth 2 processed-params))
54 (result-type (cdr (assq :result-type params)))
55 (full-body (org-babel-expand-body:generic
56 body params))
57 (result (org-babel-groovy-evaluate
58 session full-body result-type result-params)))
60 (org-babel-reassemble-table
61 result
62 (org-babel-pick-name
63 (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
64 (org-babel-pick-name
65 (cdr (assq :rowname-names params)) (cdr (assq :rownames params))))))
67 (defvar org-babel-groovy-wrapper-method
69 "class Runner extends Script {
70 def out = new PrintWriter(new ByteArrayOutputStream())
71 def run() { %s }
74 println(new Runner().run())
78 (defun org-babel-groovy-evaluate
79 (session body &optional result-type result-params)
80 "Evaluate BODY in external Groovy process.
81 If RESULT-TYPE equals `output' then return standard output as a string.
82 If RESULT-TYPE equals `value' then return the value of the last statement
83 in BODY as elisp."
84 (when session (error "Sessions are not (yet) supported for Groovy"))
85 (pcase result-type
86 (`output
87 (let ((src-file (org-babel-temp-file "groovy-")))
88 (progn (with-temp-file src-file (insert body))
89 (org-babel-eval
90 (concat org-babel-groovy-command " " src-file) ""))))
91 (`value
92 (let* ((src-file (org-babel-temp-file "groovy-"))
93 (wrapper (format org-babel-groovy-wrapper-method body)))
94 (with-temp-file src-file (insert wrapper))
95 (let ((raw (org-babel-eval
96 (concat org-babel-groovy-command " " src-file) "")))
97 (org-babel-result-cond result-params
98 raw
99 (org-babel-script-escape raw)))))))
102 (defun org-babel-prep-session:groovy (_session _params)
103 "Prepare SESSION according to the header arguments specified in PARAMS."
104 (error "Sessions are not (yet) supported for Groovy"))
106 (defun org-babel-groovy-initiate-session (&optional _session)
107 "If there is not a current inferior-process-buffer in SESSION
108 then create. Return the initialized session. Sessions are not
109 supported in Groovy."
110 nil)
112 (provide 'ob-groovy)
116 ;;; ob-groovy.el ends here