ob-scala.el 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. ;;; ob-scala.el --- Babel Functions for Scala -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2012-2017 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. (defvar org-babel-tangle-lang-exts) ;; Autoloaded
  26. (add-to-list 'org-babel-tangle-lang-exts '("scala" . "scala"))
  27. (defvar org-babel-default-header-args:scala '())
  28. (defvar org-babel-scala-command "scala"
  29. "Name of the command to use for executing Scala code.")
  30. (defun org-babel-execute:scala (body params)
  31. "Execute a block of Scala code with org-babel. This function is
  32. called by `org-babel-execute-src-block'"
  33. (message "executing Scala source code block")
  34. (let* ((processed-params (org-babel-process-params params))
  35. (session (org-babel-scala-initiate-session (nth 0 processed-params)))
  36. (result-params (nth 2 processed-params))
  37. (result-type (cdr (assq :result-type params)))
  38. (full-body (org-babel-expand-body:generic
  39. body params))
  40. (result (org-babel-scala-evaluate
  41. session full-body result-type result-params)))
  42. (org-babel-reassemble-table
  43. result
  44. (org-babel-pick-name
  45. (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
  46. (org-babel-pick-name
  47. (cdr (assq :rowname-names params)) (cdr (assq :rownames params))))))
  48. (defvar org-babel-scala-wrapper-method
  49. "var str_result :String = null;
  50. Console.withOut(new java.io.OutputStream() {def write(b: Int){
  51. }}) {
  52. str_result = {
  53. %s
  54. }.toString
  55. }
  56. print(str_result)
  57. ")
  58. (defun org-babel-scala-evaluate
  59. (session body &optional result-type result-params)
  60. "Evaluate BODY in external Scala process.
  61. If RESULT-TYPE equals `output' then return standard output as a string.
  62. If RESULT-TYPE equals `value' then return the value of the last statement
  63. in BODY as elisp."
  64. (when session (error "Sessions are not (yet) supported for Scala"))
  65. (pcase result-type
  66. (`output
  67. (let ((src-file (org-babel-temp-file "scala-")))
  68. (with-temp-file src-file (insert body))
  69. (org-babel-eval
  70. (concat org-babel-scala-command " " src-file) "")))
  71. (`value
  72. (let* ((src-file (org-babel-temp-file "scala-"))
  73. (wrapper (format org-babel-scala-wrapper-method body)))
  74. (with-temp-file src-file (insert wrapper))
  75. (let ((raw (org-babel-eval
  76. (concat org-babel-scala-command " " src-file) "")))
  77. (org-babel-result-cond result-params
  78. raw
  79. (org-babel-script-escape raw)))))))
  80. (defun org-babel-prep-session:scala (_session _params)
  81. "Prepare SESSION according to the header arguments specified in PARAMS."
  82. (error "Sessions are not (yet) supported for Scala"))
  83. (defun org-babel-scala-initiate-session (&optional _session)
  84. "If there is not a current inferior-process-buffer in SESSION
  85. then create. Return the initialized session. Sessions are not
  86. supported in Scala."
  87. nil)
  88. (provide 'ob-scala)
  89. ;;; ob-scala.el ends here