ob-clojure.el 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. ;;; ob-clojure.el --- org-babel functions for clojure evaluation
  2. ;; Copyright (C) 2009-2013 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 for all eval
  21. ;;; Requirements:
  22. ;;; - clojure (at least 1.2.0)
  23. ;;; - clojure-mode
  24. ;;; - either slime or nrepl
  25. ;;; For SLIME-way, the best way to install these components is by
  26. ;;; following the directions as set out by Phil Hagelberg (Technomancy)
  27. ;;; on the web page: http://technomancy.us/126
  28. ;;; For nREPL-way:
  29. ;;; get clojure is with https://github.com/technomancy/leiningen
  30. ;;; get nrepl from MELPA (clojure-mode is a dependency).
  31. ;;; Code:
  32. (require 'ob)
  33. (declare-function slime-eval "ext:slime" (sexp &optional package))
  34. (declare-function nrepl-current-connection-buffer "ext:nrepl" ())
  35. (declare-function nrepl-eval "ext:nrepl" (body))
  36. (defvar org-babel-tangle-lang-exts)
  37. (add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
  38. (defvar org-babel-default-header-args:clojure '())
  39. (defvar org-babel-header-args:clojure '((package . :any)))
  40. (defcustom org-babel-clojure-backend 'nrepl
  41. "Backend used to evaluate Clojure code blocks."
  42. :group 'org-babel
  43. :type 'symbol)
  44. (defun org-babel-expand-body:clojure (body params)
  45. "Expand BODY according to PARAMS, return the expanded body."
  46. (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  47. (result-params (cdr (assoc :result-params params)))
  48. (print-level nil) (print-length nil)
  49. (body (org-babel-trim
  50. (if (> (length vars) 0)
  51. (concat "(let ["
  52. (mapconcat
  53. (lambda (var)
  54. (format "%S (quote %S)" (car var) (cdr var)))
  55. vars "\n ")
  56. "]\n" body ")")
  57. body))))
  58. (cond ((or (member "code" result-params) (member "pp" result-params))
  59. (format (concat "(let [org-mode-print-catcher (java.io.StringWriter.)] "
  60. "(clojure.pprint/with-pprint-dispatch clojure.pprint/%s-dispatch "
  61. "(clojure.pprint/pprint (do %s) org-mode-print-catcher) "
  62. "(str org-mode-print-catcher)))")
  63. (if (member "code" result-params) "code" "simple") body))
  64. ;; if (:results output), collect printed output
  65. ((member "output" result-params)
  66. (format "(clojure.core/with-out-str %s)" body))
  67. (t body))))
  68. (defun org-babel-execute:clojure (body params)
  69. "Execute a block of Clojure code with Babel."
  70. (let ((expanded (org-babel-expand-body:clojure body params)))
  71. (case org-babel-clojure-backend
  72. (slime
  73. (require 'slime)
  74. (with-temp-buffer
  75. (insert expanded)
  76. ((lambda (result)
  77. (let ((result-params (cdr (assoc :result-params params))))
  78. (org-babel-result-cond result-params
  79. result
  80. (condition-case nil (org-babel-script-escape result)
  81. (error result)))))
  82. (slime-eval
  83. `(swank:eval-and-grab-output
  84. ,(buffer-substring-no-properties (point-min) (point-max)))
  85. (cdr (assoc :package params))))))
  86. (nrepl
  87. (require 'nrepl)
  88. (if (nrepl-current-connection-buffer)
  89. (let* ((result (nrepl-eval expanded))
  90. (s (plist-get result :stdout))
  91. (r (plist-get result :value)))
  92. (if s (concat s "\n" r) r))
  93. (error "nREPL not connected! Use M-x nrepl-jack-in."))))))
  94. (provide 'ob-clojure)
  95. ;;; ob-clojure.el ends here