ob-java.el 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. ;;; ob-java.el --- Babel Functions for Java -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2011-2018 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 <https://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. (defcustom org-babel-java-command "java"
  25. "Name of the java command.
  26. May be either a command in the path, like java
  27. or an absolute path name, like /usr/local/bin/java
  28. parameters may be used, like java -verbose"
  29. :group 'org-babel
  30. :version "24.3"
  31. :type 'string)
  32. (defcustom org-babel-java-compiler "javac"
  33. "Name of the java compiler.
  34. May be either a command in the path, like javac
  35. or an absolute path name, like /usr/local/bin/javac
  36. parameters may be used, like javac -verbose"
  37. :group 'org-babel
  38. :version "24.3"
  39. :type 'string)
  40. (defun org-babel-execute:java (body params)
  41. (let* ((classname (or (cdr (assq :classname params))
  42. (error
  43. "Can't compile a java block without a classname")))
  44. (packagename (file-name-directory classname))
  45. (src-file (concat classname ".java"))
  46. (cmpflag (or (cdr (assq :cmpflag params)) ""))
  47. (cmdline (or (cdr (assq :cmdline params)) ""))
  48. (full-body (org-babel-expand-body:generic body params)))
  49. (with-temp-file src-file (insert full-body))
  50. (org-babel-eval
  51. (concat org-babel-java-compiler " " cmpflag " " src-file) "")
  52. ;; created package-name directories if missing
  53. (unless (or (not packagename) (file-exists-p packagename))
  54. (make-directory packagename 'parents))
  55. (let ((results (org-babel-eval (concat org-babel-java-command
  56. " " cmdline " " classname) "")))
  57. (org-babel-reassemble-table
  58. (org-babel-result-cond (cdr (assq :result-params params))
  59. (org-babel-read results)
  60. (let ((tmp-file (org-babel-temp-file "c-")))
  61. (with-temp-file tmp-file (insert results))
  62. (org-babel-import-elisp-from-file tmp-file)))
  63. (org-babel-pick-name
  64. (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
  65. (org-babel-pick-name
  66. (cdr (assq :rowname-names params)) (cdr (assq :rownames params)))))))
  67. (provide 'ob-java)
  68. ;;; ob-java.el ends here