ob-clojure.el 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 nrepl-send-string-sync "ext:nrepl-client" (input &optional ns session))
  38. (declare-function nrepl-current-tooling-session "ext:nrepl-client" ())
  39. (declare-function nrepl-current-connection-buffer "ext:nrepl" ())
  40. (declare-function nrepl-eval "ext:nrepl" (body))
  41. (declare-function slime-eval "ext:slime" (sexp &optional package))
  42. (defvar org-babel-tangle-lang-exts)
  43. (add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
  44. (defvar org-babel-default-header-args:clojure '())
  45. (defvar org-babel-header-args:clojure '((package . :any)))
  46. (defcustom org-babel-clojure-backend
  47. (cond ((featurep 'cider) 'cider)
  48. ((featurep 'nrepl) 'nrepl)
  49. (t 'slime))
  50. "Backend used to evaluate Clojure code blocks."
  51. :group 'org-babel
  52. :type '(choice
  53. (const :tag "cider" cider)
  54. (const :tag "nrepl" nrepl)
  55. (const :tag "SLIME" slime)))
  56. (defun org-babel-expand-body:clojure (body params)
  57. "Expand BODY according to PARAMS, return the expanded body."
  58. (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  59. (result-params (cdr (assoc :result-params params)))
  60. (print-level nil) (print-length nil)
  61. (body (org-babel-trim
  62. (if (> (length vars) 0)
  63. (concat "(let ["
  64. (mapconcat
  65. (lambda (var)
  66. (format "%S (quote %S)" (car var) (cdr var)))
  67. vars "\n ")
  68. "]\n" body ")")
  69. body))))
  70. (cond ((or (member "code" result-params) (member "pp" result-params))
  71. (format (concat "(let [org-mode-print-catcher (java.io.StringWriter.)] "
  72. "(clojure.pprint/with-pprint-dispatch clojure.pprint/%s-dispatch "
  73. "(clojure.pprint/pprint (do %s) org-mode-print-catcher) "
  74. "(str org-mode-print-catcher)))")
  75. (if (member "code" result-params) "code" "simple") body))
  76. ;; if (:results output), collect printed output
  77. ((member "output" result-params)
  78. (format "(clojure.core/with-out-str %s)" body))
  79. (t body))))
  80. (defun org-babel-execute:clojure (body params)
  81. "Execute a block of Clojure code with Babel."
  82. (let ((expanded (org-babel-expand-body:clojure body params)))
  83. (case org-babel-clojure-backend
  84. (cider
  85. (require 'cider)
  86. (or (nth 1 (nrepl-send-string-sync
  87. expanded
  88. (cider-current-ns)
  89. (nrepl-current-tooling-session)))
  90. (error "nREPL not connected! Use M-x cider-jack-in RET")))
  91. (nrepl
  92. (require 'nrepl)
  93. (if (nrepl-current-connection-buffer)
  94. (let* ((result (nrepl-eval expanded))
  95. (s (plist-get result :stdout))
  96. (r (plist-get result :value)))
  97. (if s (concat s "\n" r) r))
  98. (error "nREPL not connected! Use M-x nrepl-jack-in RET")))
  99. (slime
  100. (require 'slime)
  101. (with-temp-buffer
  102. (insert expanded)
  103. ((lambda (result)
  104. (let ((result-params (cdr (assoc :result-params params))))
  105. (org-babel-result-cond result-params
  106. result
  107. (condition-case nil (org-babel-script-escape result)
  108. (error result)))))
  109. (slime-eval
  110. `(swank:eval-and-grab-output
  111. ,(buffer-substring-no-properties (point-min) (point-max)))
  112. (cdr (assoc :package params)))))))))
  113. (provide 'ob-clojure)
  114. ;;; ob-clojure.el ends here