ob-scala.el 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ;;; ob-scala.el --- org-babel functions for Scala evaluation
  2. ;; Copyright (C) Andrzej Lichnerowicz
  3. ;; Author: Andrzej Lichnerowicz
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; Currently only supports the external execution. No session support yet.
  24. ;;; Requirements:
  25. ;; - Scala language :: http://www.scala-lang.org/
  26. ;; - Scala major mode :: Can be installed from Scala sources
  27. ;; https://github.com/scala/scala-dist/blob/master/tool-support/src/emacs/scala-mode.el
  28. ;;; Code:
  29. (require 'ob)
  30. (require 'ob-ref)
  31. (require 'ob-comint)
  32. (require 'ob-eval)
  33. (add-to-list 'org-babel-tangle-lang-exts '("scala" . "scala"))
  34. (defvar org-babel-default-header-args:scala '())
  35. (defvar org-babel-scala-command "scala"
  36. "Name of the command to use for executing Scala code.")
  37. (defun org-babel-execute:scala (body params)
  38. "Execute a block of Scala code with org-babel. This function is
  39. called by `org-babel-execute-src-block'"
  40. (message "executing Scala source code block")
  41. (let* ((processed-params (org-babel-process-params params))
  42. (session (org-babel-scala-initiate-session (first processed-params)))
  43. (vars (second processed-params))
  44. (result-params (third processed-params))
  45. (result-type (cdr (assoc :result-type params)))
  46. (full-body (org-babel-expand-body:generic
  47. body params))
  48. (result (org-babel-scala-evaluate
  49. session full-body result-type result-params)))
  50. (org-babel-reassemble-table
  51. result
  52. (org-babel-pick-name
  53. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  54. (org-babel-pick-name
  55. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
  56. (defun org-babel-scala-table-or-string (results)
  57. "Convert RESULTS into an appropriate elisp value.
  58. If RESULTS look like a table, then convert them into an
  59. Emacs-lisp table, otherwise return the results as a string."
  60. (org-babel-script-escape results))
  61. (defvar org-babel-scala-wrapper-method
  62. "(
  63. %s
  64. ) asString print
  65. ")
  66. (defun org-babel-scala-evaluate (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 supported for Scala. Yet."))
  72. (case result-type
  73. (output
  74. (let ((src-file (org-babel-temp-file "scala-")))
  75. (progn (with-temp-file src-file (insert full-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. ((lambda (raw)
  83. (if (member "code" result-params)
  84. raw
  85. (org-babel-scala-table-or-string raw)))
  86. (org-babel-eval
  87. (concat org-babel-scala-command " " src-file) ""))))))
  88. (defun org-babel-prep-session:scala (session params)
  89. "Prepare SESSION according to the header arguments specified in PARAMS."
  90. (error "Sessions are not supported for Scala. Yet."))
  91. (defun org-babel-scala-initiate-session (&optional session)
  92. "If there is not a current inferior-process-buffer in SESSION
  93. then create. Return the initialized session. Sessions are not
  94. supported in Scala."
  95. nil)
  96. (provide 'ob-scala)
  97. ;;; ob-scala.el ends here