ORG-NEWS: Backport commit 62803a2ef from Emacs
[org-mode.git] / lisp / ob-scala.el
blob7d5f299ec6237db47a6ef90fc6d36503fff6056b
1 ;;; ob-scala.el --- Babel Functions for Scala -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2012-2017 Free Software Foundation, Inc.
5 ;; Author: Andrzej Lichnerowicz
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 ;; - Scala language :: http://www.scala-lang.org/
29 ;; - Scala major mode :: Can be installed from Scala sources
30 ;; https://github.com/scala/scala-dist/blob/master/tool-support/src/emacs/scala-mode.el
32 ;;; Code:
33 (require 'ob)
35 (defvar org-babel-tangle-lang-exts) ;; Autoloaded
36 (add-to-list 'org-babel-tangle-lang-exts '("scala" . "scala"))
37 (defvar org-babel-default-header-args:scala '())
38 (defvar org-babel-scala-command "scala"
39 "Name of the command to use for executing Scala code.")
41 (defun org-babel-execute:scala (body params)
42 "Execute a block of Scala code with org-babel. This function is
43 called by `org-babel-execute-src-block'"
44 (message "executing Scala source code block")
45 (let* ((processed-params (org-babel-process-params params))
46 (session (org-babel-scala-initiate-session (nth 0 processed-params)))
47 (result-params (nth 2 processed-params))
48 (result-type (cdr (assq :result-type params)))
49 (full-body (org-babel-expand-body:generic
50 body params))
51 (result (org-babel-scala-evaluate
52 session full-body result-type result-params)))
54 (org-babel-reassemble-table
55 result
56 (org-babel-pick-name
57 (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
58 (org-babel-pick-name
59 (cdr (assq :rowname-names params)) (cdr (assq :rownames params))))))
61 (defvar org-babel-scala-wrapper-method
63 "var str_result :String = null;
65 Console.withOut(new java.io.OutputStream() {def write(b: Int){
66 }}) {
67 str_result = {
69 }.toString
72 print(str_result)
76 (defun org-babel-scala-evaluate
77 (session body &optional result-type result-params)
78 "Evaluate BODY in external Scala process.
79 If RESULT-TYPE equals `output' then return standard output as a string.
80 If RESULT-TYPE equals `value' then return the value of the last statement
81 in BODY as elisp."
82 (when session (error "Sessions are not (yet) supported for Scala"))
83 (pcase result-type
84 (`output
85 (let ((src-file (org-babel-temp-file "scala-")))
86 (with-temp-file src-file (insert body))
87 (org-babel-eval
88 (concat org-babel-scala-command " " src-file) "")))
89 (`value
90 (let* ((src-file (org-babel-temp-file "scala-"))
91 (wrapper (format org-babel-scala-wrapper-method body)))
92 (with-temp-file src-file (insert wrapper))
93 (let ((raw (org-babel-eval
94 (concat org-babel-scala-command " " src-file) "")))
95 (org-babel-result-cond result-params
96 raw
97 (org-babel-script-escape raw)))))))
100 (defun org-babel-prep-session:scala (_session _params)
101 "Prepare SESSION according to the header arguments specified in PARAMS."
102 (error "Sessions are not (yet) supported for Scala"))
104 (defun org-babel-scala-initiate-session (&optional _session)
105 "If there is not a current inferior-process-buffer in SESSION
106 then create. Return the initialized session. Sessions are not
107 supported in Scala."
108 nil)
110 (provide 'ob-scala)
114 ;;; ob-scala.el ends here