config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

ob-groovy.el (4317B)


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