|
| 1 | +;;; ob-scala.el --- Babel Functions for Scala -*- lexical-binding: t; -*- |
| 2 | + |
| 3 | +;; Copyright (C) 2012-2017 Free Software Foundation, Inc. |
| 4 | + |
| 5 | +;; Author: Andrzej Lichnerowicz |
| 6 | +;; Keywords: literate programming, reproducible research |
| 7 | +;; Homepage: http://orgmode.org |
| 8 | + |
| 9 | + |
| 10 | +;; GNU Emacs is free software: you can redistribute it and/or modify |
| 11 | +;; it under the terms of the GNU General Public License as published by |
| 12 | +;; the Free Software Foundation, either version 3 of the License, or |
| 13 | +;; (at your option) any later version. |
| 14 | + |
| 15 | +;; GNU Emacs is distributed in the hope that it will be useful, |
| 16 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | +;; GNU General Public License for more details. |
| 19 | + |
| 20 | +;; You should have received a copy of the GNU General Public License |
| 21 | +;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
| 22 | + |
| 23 | +;;; Commentary: |
| 24 | +;; Currently only supports the external execution. No session support yet. |
| 25 | + |
| 26 | +;;; Requirements: |
| 27 | +;; - Scala language :: http://www.scala-lang.org/ |
| 28 | +;; - Scala major mode :: https://github.com/hvesalai/emacs-scala-mode |
| 29 | + |
| 30 | +;;; Code: |
| 31 | +(require 'ob) |
| 32 | + |
| 33 | +(defvar org-babel-tangle-lang-exts) ;; Autoloaded |
| 34 | +(add-to-list 'org-babel-tangle-lang-exts '("scala" . "scala")) |
| 35 | +(defvar org-babel-default-header-args:scala '()) |
| 36 | +(defvar org-babel-scala-command "scala" |
| 37 | + "Name of the command to use for executing Scala code.") |
| 38 | + |
| 39 | +(defun org-babel-execute:scala (body params) |
| 40 | + "Execute a block of Scala code with org-babel. This function is |
| 41 | +called by `org-babel-execute-src-block'" |
| 42 | + (message "executing Scala source code block") |
| 43 | + (let* ((processed-params (org-babel-process-params params)) |
| 44 | + (session (org-babel-scala-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-scala-evaluate |
| 50 | + session full-body result-type result-params))) |
| 51 | + |
| 52 | + (org-babel-reassemble-table |
| 53 | + result |
| 54 | + (org-babel-pick-name |
| 55 | + (cdr (assq :colname-names params)) (cdr (assq :colnames params))) |
| 56 | + (org-babel-pick-name |
| 57 | + (cdr (assq :rowname-names params)) (cdr (assq :rownames params)))))) |
| 58 | + |
| 59 | +(defvar org-babel-scala-wrapper-method |
| 60 | + |
| 61 | +"var str_result :String = null; |
| 62 | +Console.withOut(new java.io.OutputStream() {def write(b: Int){ |
| 63 | +}}) { |
| 64 | + str_result = { |
| 65 | +%s |
| 66 | + }.toString |
| 67 | +} |
| 68 | +print(str_result) |
| 69 | +") |
| 70 | + |
| 71 | + |
| 72 | +(defun org-babel-scala-evaluate |
| 73 | + (session body &optional result-type result-params) |
| 74 | + "Evaluate BODY in external Scala process. |
| 75 | +If RESULT-TYPE equals `output' then return standard output as a string. |
| 76 | +If RESULT-TYPE equals `value' then return the value of the last statement |
| 77 | +in BODY as elisp." |
| 78 | + (when session (error "Sessions are not (yet) supported for Scala")) |
| 79 | + (pcase result-type |
| 80 | + (`output |
| 81 | + (let ((src-file (org-babel-temp-file "scala-"))) |
| 82 | + (with-temp-file src-file (insert body)) |
| 83 | + (org-babel-eval |
| 84 | + (concat org-babel-scala-command " " src-file) ""))) |
| 85 | + (`value |
| 86 | + (let* ((src-file (org-babel-temp-file "scala-")) |
| 87 | + (wrapper (format org-babel-scala-wrapper-method body))) |
| 88 | + (with-temp-file src-file (insert wrapper)) |
| 89 | + (let ((raw (org-babel-eval |
| 90 | + (concat org-babel-scala-command " " src-file) ""))) |
| 91 | + (org-babel-result-cond result-params |
| 92 | + raw |
| 93 | + (org-babel-script-escape raw))))))) |
| 94 | + |
| 95 | + |
| 96 | +(defun org-babel-prep-session:scala (_session _params) |
| 97 | + "Prepare SESSION according to the header arguments specified in PARAMS." |
| 98 | + (error "Sessions are not (yet) supported for Scala")) |
| 99 | + |
| 100 | +(defun org-babel-scala-initiate-session (&optional _session) |
| 101 | + "If there is not a current inferior-process-buffer in SESSION |
| 102 | +then create. Return the initialized session. Sessions are not |
| 103 | +supported in Scala." |
| 104 | + nil) |
| 105 | + |
| 106 | +(provide 'ob-scala) |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +;;; ob-scala.el ends here |
0 commit comments