ob-clojure.el 4.2 KB

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