Commit | Line | Data |
---|---|---|
8223b1d2 BG |
1 | ;;; ob-scala.el --- org-babel functions for Scala evaluation |
2 | ||
ba318903 | 3 | ;; Copyright (C) 2012-2014 Free Software Foundation, Inc. |
8223b1d2 BG |
4 | |
5 | ;; Author: Andrzej Lichnerowicz | |
6 | ;; Keywords: literate programming, reproducible research | |
7 | ;; Homepage: http://orgmode.org | |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
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. | |
15 | ||
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. | |
20 | ||
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/>. | |
23 | ||
24 | ;;; Commentary: | |
25 | ;; Currently only supports the external execution. No session support yet. | |
26 | ||
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 | |
31 | ||
32 | ;;; Code: | |
33 | (require 'ob) | |
8223b1d2 BG |
34 | (eval-when-compile (require 'cl)) |
35 | ||
bdebdb64 | 36 | (defvar org-babel-tangle-lang-exts) ;; Autoloaded |
8223b1d2 BG |
37 | (add-to-list 'org-babel-tangle-lang-exts '("scala" . "scala")) |
38 | (defvar org-babel-default-header-args:scala '()) | |
39 | (defvar org-babel-scala-command "scala" | |
40 | "Name of the command to use for executing Scala code.") | |
41 | ||
8223b1d2 BG |
42 | (defun org-babel-execute:scala (body params) |
43 | "Execute a block of Scala code with org-babel. This function is | |
44 | called by `org-babel-execute-src-block'" | |
45 | (message "executing Scala source code block") | |
46 | (let* ((processed-params (org-babel-process-params params)) | |
47 | (session (org-babel-scala-initiate-session (nth 0 processed-params))) | |
48 | (vars (nth 1 processed-params)) | |
49 | (result-params (nth 2 processed-params)) | |
50 | (result-type (cdr (assoc :result-type params))) | |
51 | (full-body (org-babel-expand-body:generic | |
52 | body params)) | |
53 | (result (org-babel-scala-evaluate | |
54 | session full-body result-type result-params))) | |
55 | ||
56 | (org-babel-reassemble-table | |
57 | result | |
58 | (org-babel-pick-name | |
59 | (cdr (assoc :colname-names params)) (cdr (assoc :colnames params))) | |
60 | (org-babel-pick-name | |
61 | (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))) | |
62 | ||
63 | ||
64 | (defun org-babel-scala-table-or-string (results) | |
65 | "Convert RESULTS into an appropriate elisp value. | |
66 | If RESULTS look like a table, then convert them into an | |
67 | Emacs-lisp table, otherwise return the results as a string." | |
68 | (org-babel-script-escape results)) | |
69 | ||
70 | ||
71 | (defvar org-babel-scala-wrapper-method | |
bdebdb64 BG |
72 | |
73 | "var str_result :String = null; | |
74 | ||
75 | Console.withOut(new java.io.OutputStream() {def write(b: Int){ | |
76 | }}) { | |
77 | str_result = { | |
8223b1d2 | 78 | %s |
bdebdb64 BG |
79 | }.toString |
80 | } | |
81 | ||
82 | print(str_result) | |
8223b1d2 BG |
83 | ") |
84 | ||
85 | ||
86 | (defun org-babel-scala-evaluate | |
87 | (session body &optional result-type result-params) | |
88 | "Evaluate BODY in external Scala process. | |
89 | If RESULT-TYPE equals 'output then return standard output as a string. | |
90 | If RESULT-TYPE equals 'value then return the value of the last statement | |
91 | in BODY as elisp." | |
92 | (when session (error "Sessions are not (yet) supported for Scala")) | |
93 | (case result-type | |
94 | (output | |
95 | (let ((src-file (org-babel-temp-file "scala-"))) | |
96 | (progn (with-temp-file src-file (insert body)) | |
97 | (org-babel-eval | |
98 | (concat org-babel-scala-command " " src-file) "")))) | |
99 | (value | |
100 | (let* ((src-file (org-babel-temp-file "scala-")) | |
101 | (wrapper (format org-babel-scala-wrapper-method body))) | |
102 | (with-temp-file src-file (insert wrapper)) | |
666ffc7e SM |
103 | (let ((raw (org-babel-eval |
104 | (concat org-babel-scala-command " " src-file) ""))) | |
105 | (org-babel-result-cond result-params | |
106 | raw | |
107 | (org-babel-scala-table-or-string raw))))))) | |
8223b1d2 BG |
108 | |
109 | ||
110 | (defun org-babel-prep-session:scala (session params) | |
111 | "Prepare SESSION according to the header arguments specified in PARAMS." | |
112 | (error "Sessions are not (yet) supported for Scala")) | |
113 | ||
114 | (defun org-babel-scala-initiate-session (&optional session) | |
115 | "If there is not a current inferior-process-buffer in SESSION | |
116 | then create. Return the initialized session. Sessions are not | |
117 | supported in Scala." | |
118 | nil) | |
119 | ||
120 | (provide 'ob-scala) | |
121 | ||
122 | ||
123 | ||
124 | ;;; ob-scala.el ends here |