ob-java.el 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. ;;; ob-java.el --- org-babel functions for java evaluation
  2. ;; Copyright (C) 2011-2014 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  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 this only supports the external compilation and execution
  19. ;; of java code blocks (i.e., no session support).
  20. ;;; Code:
  21. (require 'ob)
  22. (defvar org-babel-tangle-lang-exts)
  23. (add-to-list 'org-babel-tangle-lang-exts '("java" . "java"))
  24. (defvar org-babel-java-command "java"
  25. "Name of the java command.")
  26. (defvar org-babel-java-compiler "javac"
  27. "Name of the java compiler.")
  28. (defun org-babel-execute:java (body params)
  29. (let* ((classname (or (cdr (assoc :classname params))
  30. (error
  31. "Can't compile a java block without a classname")))
  32. (packagename (file-name-directory classname))
  33. (src-file (concat classname ".java"))
  34. (cmpflag (or (cdr (assoc :cmpflag params)) ""))
  35. (cmdline (or (cdr (assoc :cmdline params)) ""))
  36. (full-body (org-babel-expand-body:generic body params))
  37. (compile
  38. (progn (with-temp-file src-file (insert full-body))
  39. (org-babel-eval
  40. (concat org-babel-java-compiler
  41. " " cmpflag " " src-file) ""))))
  42. ;; created package-name directories if missing
  43. (unless (or (not packagename) (file-exists-p packagename))
  44. (make-directory packagename 'parents))
  45. (let ((results (org-babel-eval (concat org-babel-java-command
  46. " " cmdline " " classname) "")))
  47. (org-babel-reassemble-table
  48. (org-babel-result-cond (cdr (assoc :result-params params))
  49. (org-babel-read results)
  50. (let ((tmp-file (org-babel-temp-file "c-")))
  51. (with-temp-file tmp-file (insert results))
  52. (org-babel-import-elisp-from-file tmp-file)))
  53. (org-babel-pick-name
  54. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  55. (org-babel-pick-name
  56. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))))
  57. (provide 'ob-java)
  58. ;;; ob-java.el ends here