ob-scala.el 4.0 KB

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