ob-clojure.el 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. ;;; ob-clojure.el --- org-babel functions for clojure evaluation
  2. ;; Copyright (C) 2009-2014 Free Software Foundation, Inc.
  3. ;; Author: Joel Boehland, Eric Schulte, Oleh Krehel
  4. ;;
  5. ;; Keywords: literate programming, reproducible research
  6. ;; Homepage: http://orgmode.org
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs 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 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Support for evaluating clojure code, relies either on Slime or
  20. ;; on Nrepl.el for all eval.
  21. ;; Requirements:
  22. ;; - clojure (at least 1.2.0)
  23. ;; - clojure-mode
  24. ;; - either cider or nrepl.el or SLIME
  25. ;; For cider, see https://github.com/clojure-emacs/cider
  26. ;; For SLIME, the best way to install these components is by following
  27. ;; the directions as set out by Phil Hagelberg (Technomancy) on the
  28. ;; web page: http://technomancy.us/126
  29. ;; For nREPL:
  30. ;; get clojure with https://github.com/technomancy/leiningen
  31. ;; get nrepl from MELPA (clojure-mode is a dependency).
  32. ;;; Code:
  33. (require 'ob)
  34. (eval-when-compile
  35. (require 'cl))
  36. (declare-function cider-current-ns "ext:cider-interaction" ())
  37. (declare-function cider-get-raw-value "ext:cider-client" (eval-result))
  38. (declare-function cider-eval-sync "ext:cider-client" (input &optional ns session))
  39. (declare-function nrepl-send-string-sync "ext:nrepl-client" (input &optional ns session))
  40. (declare-function nrepl-current-tooling-session "ext:nrepl-client" ())
  41. (declare-function nrepl-current-connection-buffer "ext:nrepl" ())
  42. (declare-function nrepl-eval "ext:nrepl" (body))
  43. (declare-function slime-eval "ext:slime" (sexp &optional package))
  44. (defvar org-babel-tangle-lang-exts)
  45. (add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
  46. (defvar org-babel-default-header-args:clojure '())
  47. (defvar org-babel-header-args:clojure '((package . :any)))
  48. (defcustom org-babel-clojure-backend
  49. (cond ((featurep 'cider) 'cider)
  50. ((featurep 'nrepl) 'nrepl)
  51. (t 'slime))
  52. "Backend used to evaluate Clojure code blocks."
  53. :group 'org-babel
  54. :type '(choice
  55. (const :tag "cider" cider)
  56. (const :tag "nrepl" nrepl)
  57. (const :tag "SLIME" slime)))
  58. (defun org-babel-expand-body:clojure (body params)
  59. "Expand BODY according to PARAMS, return the expanded body."
  60. (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  61. (result-params (cdr (assoc :result-params params)))
  62. (print-level nil) (print-length nil)
  63. (body (org-babel-trim
  64. (if (> (length vars) 0)
  65. (concat "(let ["
  66. (mapconcat
  67. (lambda (var)
  68. (format "%S (quote %S)" (car var) (cdr var)))
  69. vars "\n ")
  70. "]\n" body ")")
  71. body))))
  72. (cond ((or (member "code" result-params) (member "pp" result-params))
  73. (format (concat "(let [org-mode-print-catcher (java.io.StringWriter.)] "
  74. "(clojure.pprint/with-pprint-dispatch clojure.pprint/%s-dispatch "
  75. "(clojure.pprint/pprint (do %s) org-mode-print-catcher) "
  76. "(str org-mode-print-catcher)))")
  77. (if (member "code" result-params) "code" "simple") body))
  78. ;; if (:results output), collect printed output
  79. ((member "output" result-params)
  80. (format "(clojure.core/with-out-str %s)" body))
  81. (t body))))
  82. (defun org-babel-execute:clojure (body params)
  83. "Execute a block of Clojure code with Babel."
  84. (let ((expanded (org-babel-expand-body:clojure body params))
  85. result)
  86. (case org-babel-clojure-backend
  87. (cider
  88. (require 'cider)
  89. (setq result
  90. (or (cider-get-raw-value (cider-eval-sync expanded))
  91. (error "nREPL not connected! Use M-x cider-jack-in RET"))))
  92. (nrepl
  93. (require 'nrepl)
  94. (setq result
  95. (if (nrepl-current-connection-buffer)
  96. (let* ((result (nrepl-eval expanded))
  97. (s (plist-get result :stdout))
  98. (r (plist-get result :value)))
  99. (if s (concat s "\n" r) r))
  100. (error "nREPL not connected! Use M-x nrepl-jack-in RET"))))
  101. (slime
  102. (require 'slime)
  103. (with-temp-buffer
  104. (insert expanded)
  105. (setq result
  106. (slime-eval
  107. `(swank:eval-and-grab-output
  108. ,(buffer-substring-no-properties (point-min) (point-max)))
  109. (cdr (assoc :package params)))))))
  110. (org-babel-result-cond (cdr (assoc :result-params params))
  111. result
  112. (condition-case nil (org-babel-script-escape result)
  113. (error result)))))
  114. (provide 'ob-clojure)
  115. ;;; ob-clojure.el ends here