ob-scala.el 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. ;;; ob-scala.el --- org-babel functions for Scala evaluation
  2. ;; Copyright (C) 2012-2014 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. (vars (nth 1 processed-params))
  38. (result-params (nth 2 processed-params))
  39. (result-type (cdr (assoc :result-type params)))
  40. (full-body (org-babel-expand-body:generic
  41. body params))
  42. (result (org-babel-scala-evaluate
  43. session full-body result-type result-params)))
  44. (org-babel-reassemble-table
  45. result
  46. (org-babel-pick-name
  47. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  48. (org-babel-pick-name
  49. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
  50. (defun org-babel-scala-table-or-string (results)
  51. "Convert RESULTS into an appropriate elisp value.
  52. If RESULTS look like a table, then convert them into an
  53. Emacs-lisp table, otherwise return the results as a string."
  54. (org-babel-script-escape results))
  55. (defvar org-babel-scala-wrapper-method
  56. "var str_result :String = null;
  57. Console.withOut(new java.io.OutputStream() {def write(b: Int){
  58. }}) {
  59. str_result = {
  60. %s
  61. }.toString
  62. }
  63. print(str_result)
  64. ")
  65. (defun org-babel-scala-evaluate
  66. (session body &optional result-type result-params)
  67. "Evaluate BODY in external Scala process.
  68. If RESULT-TYPE equals 'output then return standard output as a string.
  69. If RESULT-TYPE equals 'value then return the value of the last statement
  70. in BODY as elisp."
  71. (when session (error "Sessions are not (yet) supported for Scala"))
  72. (case result-type
  73. (output
  74. (let ((src-file (org-babel-temp-file "scala-")))
  75. (progn (with-temp-file src-file (insert body))
  76. (org-babel-eval
  77. (concat org-babel-scala-command " " src-file) ""))))
  78. (value
  79. (let* ((src-file (org-babel-temp-file "scala-"))
  80. (wrapper (format org-babel-scala-wrapper-method body)))
  81. (with-temp-file src-file (insert wrapper))
  82. (let ((raw (org-babel-eval
  83. (concat org-babel-scala-command " " src-file) "")))
  84. (org-babel-result-cond result-params
  85. raw
  86. (org-babel-scala-table-or-string raw)))))))
  87. (defun org-babel-prep-session:scala (session params)
  88. "Prepare SESSION according to the header arguments specified in PARAMS."
  89. (error "Sessions are not (yet) supported for Scala"))
  90. (defun org-babel-scala-initiate-session (&optional session)
  91. "If there is not a current inferior-process-buffer in SESSION
  92. then create. Return the initialized session. Sessions are not
  93. supported in Scala."
  94. nil)
  95. (provide 'ob-scala)
  96. ;;; ob-scala.el ends here