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